[other]Send email with Authentication
来源:互联网 发布:淘宝兽药二维码 编辑:程序博客网 时间:2024/05/22 03:41
一个发Email的小程序,使用Gmail,需要认证.
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class SendMailUsingAuthentication {
- private static final String SMTP_AUTH_USER = "user name";
- private static final String SMTP_AUTH_PWD = "password";
- private static final String emailMsgTxt = "MsgTxt";
- private static final String emailSubjectTxt = "Subject";
- private static final String emailFromAddress = "userxxx@gmail.com";
- private static final String[] emailList = { "userxxx@gmail.com" };
- public static void main(String args[]) throws Exception {
- SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
- smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
- System.out.println("Sucessfully Sent mail to All Users");
- }
- public void postMail(String recipients[], String subject, String message, String from) throws MessagingException {
- boolean debug = false;
- final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
- // Set the host smtp address
- Properties props = new Properties();
- props.setProperty("mail.smtp.host", "smtp.gmail.com");
- props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
- props.setProperty("mail.smtp.socketFactory.fallback", "false");
- props.setProperty("mail.smtp.port", "465");
- props.setProperty("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.auth", "true");
- Authenticator auth = new SMTPAuthenticator();
- Session session = Session.getDefaultInstance(props, auth);
- session.setDebug(debug);
- // create a message
- Message msg = new MimeMessage(session);
- // set the from and to address
- InternetAddress addressFrom = new InternetAddress(from);
- msg.setFrom(addressFrom);
- InternetAddress[] addressTo = new InternetAddress[recipients.length];
- for (int i = 0; i < recipients.length; i++) {
- addressTo[i] = new InternetAddress(recipients[i]);
- }
- msg.setRecipients(Message.RecipientType.TO, addressTo);
- // Setting the Subject and Content Type
- msg.setSubject(subject);
- msg.setContent(message, "text/plain");
- Transport.send(msg);
- }
- /**
- * SimpleAuthenticator is used to do simple authentication when the SMTP
- * server requires it.
- */
- private class SMTPAuthenticator extends javax.mail.Authenticator {
- public PasswordAuthentication getPasswordAuthentication() {
- String username = SMTP_AUTH_USER;
- String password = SMTP_AUTH_PWD;
- return new PasswordAuthentication(username, password);
- }
- }
- }
- [other]Send email with Authentication
- Send email with attachment
- send email with attachment
- Java Send EMail with Attachment
- Send Email and Attachments with ASP.Net...
- send email
- send email
- Email--Send
- Send Email
- send email with velocity and spring 完整示例
- ABAP-Send email with data of internal table as attachment
- Spring v3.0.2 Learning Note 14 - Send Email with Spring
- Salesforce: How to Send Email with Default Template?
- Send Email 发送邮件
- C# 2.0 send email
- Oracle send Email
- Send email in ABAP
- Send html Email
- 中国历史上影响最大的10首诗
- Flexible Rails: Flex 3 on Rails 2
- 转载:存储器分页管理机制
- Silverlight教程第一部分: 使用Silverlight 2 和 VS 2008创建“Hello World”程序
- Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained
- [other]Send email with Authentication
- DataGridView中的数据格式失效和排序异常问题
- Linux Thin Client Networks Design and Deployment
- 新人职场日记-第一天
- The Definitive Guide to Django: Web Development Done Right
- C#中String与StringBuilder的区别
- 关于客户和项目品质
- Ubuntu for Non-Geeks, 2nd Edition: A Pain-Free, Project-Based, Get-Things-Done Guidebook [ILLUSTRATE
- 第一次