[Java] java发送邮件完整实例 java邮件工具类 →→→→→进入此内容的聊天室

来自 , 2020-10-12, 写在 Java, 查看 112 次.
URL http://www.code666.cn/view/8a3363ab
  1. import java.util.Properties;
  2.  
  3. import javax.mail.Authenticator;
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.AddressException;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13. public class SendmailUtil {
  14.        
  15.         // 设置服务器
  16.         private static String KEY_SMTP = "mail.smtp.host";
  17.         private static String VALUE_SMTP = "smtp.qq.com";
  18.         // 服务器验证
  19.         private static String KEY_PROPS = "mail.smtp.auth";
  20.         private static boolean VALUE_PROPS = true;
  21.         // 发件人用户名、密码
  22.         private String SEND_USER = "2569000943@qq.com";
  23.         private String SEND_UNAME = "2569000943";
  24.         private String SEND_PWD = "********";
  25.         // 建立会话
  26.         private MimeMessage message;
  27.         private Session s;
  28.  
  29.         /*
  30.          * 初始化方法
  31.          */
  32.         public SendmailUtil() {
  33.                 Properties props = System.getProperties();
  34.                 props.setProperty(KEY_SMTP, VALUE_SMTP);
  35.                 props.put(KEY_PROPS, "true");
  36.                 //props.put("mail.smtp.auth", "true");
  37.                 s =  Session.getDefaultInstance(props, new Authenticator(){
  38.                       protected PasswordAuthentication getPasswordAuthentication() {
  39.                           return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
  40.                       }});
  41.                 s.setDebug(true);
  42.                 message = new MimeMessage(s);
  43.         }
  44.  
  45.         /**
  46.          * 发送邮件
  47.          * 
  48.          * @param headName
  49.          *            邮件头文件名
  50.          * @param sendHtml
  51.          *            邮件内容
  52.          * @param receiveUser
  53.          *            收件人地址
  54.          */
  55.         public void doSendHtmlEmail(String headName, String sendHtml,
  56.                         String receiveUser) {
  57.                 try {
  58.                         // 发件人
  59.                         InternetAddress from = new InternetAddress(SEND_USER);
  60.                         message.setFrom(from);
  61.                         // 收件人
  62.                         InternetAddress to = new InternetAddress(receiveUser);
  63.                         message.setRecipient(Message.RecipientType.TO, to);
  64.                         // 邮件标题
  65.                         message.setSubject(headName);
  66.                         String content = sendHtml.toString();
  67.                         // 邮件内容,也可以使纯文本"text/plain"
  68.                         message.setContent(content, "text/html;charset=GBK");
  69.                         message.saveChanges();
  70.                         Transport transport = s.getTransport("smtp");
  71.                         // smtp验证,就是你用来发邮件的邮箱用户名密码
  72.                         transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
  73.                         // 发送
  74.                         transport.sendMessage(message, message.getAllRecipients());
  75.                         transport.close();
  76.                         System.out.println("send success!");
  77.                 } catch (AddressException e) {
  78.                         // TODO Auto-generated catch block
  79.                         e.printStackTrace();
  80.                 } catch (MessagingException e) {
  81.                         e.printStackTrace();
  82.                 }
  83.         }
  84.  
  85.         public static void main(String[] args) {
  86.                 SendmailUtil se = new SendmailUtil();
  87.                 se.doSendHtmlEmail("邮件头文件名""邮件内容""798210413@qq.com");
  88.         }
  89. }//源代码片段来自云代码http://yuncode.net
  90.                        
  91.  
  92. import java.security.Security;
  93. import java.util.Date;
  94. import java.util.Properties;
  95.  
  96. import javax.mail.Authenticator;
  97. import javax.mail.Message;
  98. import javax.mail.MessagingException;
  99. import javax.mail.PasswordAuthentication;
  100. import javax.mail.Session;
  101. import javax.mail.Transport;
  102. import javax.mail.internet.AddressException;
  103. import javax.mail.internet.InternetAddress;
  104. import javax.mail.internet.MimeMessage;
  105.  
  106. public class sendEmail {
  107.         public static void main(String[] args) throws AddressException,
  108.                         MessagingException {
  109.                 String SEND_USER = "2569000943@qq.com";
  110.                 String SEND_UNAME = "2569000943";
  111.                 String SEND_PWD = "********";
  112.                 String VALUE_SMTP = "smtp.qq.com";
  113.                 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  114.                 //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  115.                 // Get a Properties object
  116.                 Properties props = System.getProperties();
  117.                 // props.setProperty("mail.smtp.host", "smtp.gmail.com");
  118.                 props.setProperty("mail.smtp.host""smtp.qq.com");
  119.                 //props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  120.                 //props.setProperty("mail.smtp.socketFactory.fallback", "false");
  121.                  props.setProperty("mail.smtp.port""25");
  122.                 //props.setProperty("mail.smtp.port", "587");
  123.                 //props.setProperty("mail.smtp.socketFactory.port", "25");
  124.                 //props.setProperty("mail.smtp.socketFactory.port", "587");
  125.                 props.put("mail.smtp.auth""true");
  126.                 final String username = "2569000943";
  127.                 final String password = "chHorse123";
  128.                 Session session = Session.getDefaultInstance(props,
  129.                                 new Authenticator() {
  130.                                         protected PasswordAuthentication getPasswordAuthentication() {
  131.                                                 return new PasswordAuthentication(username, password);
  132.                                         }
  133.                                 });
  134.  
  135.                 // -- Create a new message --
  136.                 session.setDebug(true);
  137.                 Message msg = new MimeMessage(session);
  138.  
  139.                 // -- Set the FROM and TO fields --
  140.                 msg.setFrom(new InternetAddress(username + "@qq.com"));
  141.                 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
  142.                                 "798210413@qq.com"false));
  143.                 msg.setSubject("Hello---");
  144.                 msg.setText("How are you");
  145.                 msg.setSentDate(new Date());
  146.                 Transport transport = session.getTransport("smtp");
  147.                 // smtp验证,就是你用来发邮件的邮箱用户名密码
  148.                 transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
  149.                 // 发送
  150.                 transport.sendMessage(msg, msg.getAllRecipients());
  151.                 Transport.send(msg);
  152.                 transport.close();
  153.  
  154.                 System.out.println("Message sent.");
  155.         }
  156.  
  157. }//源代码片段来自云代码http://yuncode.net
  158.                        

回复 "java发送邮件完整实例 java邮件工具类"

这儿你可以回复上面这条便签

captcha