Mail sending

From , 3 Years ago, written in Java, viewed 221 times.
URL https://pastebin.vip/view/7d12b66d
  1. import org.springframework.stereotype.Component;
  2.  
  3. import com.sun.mail.util.MailSSLSocketFactory;
  4.  
  5. import javax.mail.*;
  6. import javax.mail.internet.AddressException;
  7. import javax.mail.internet.InternetAddress;
  8. import javax.mail.internet.MimeMessage;
  9.  
  10. import java.security.GeneralSecurityException;
  11. import java.util.Properties;
  12.  
  13. public class MailManager {
  14.  
  15.     //发送邮件
  16.     public static void sendMail(String email, String emailMsg)
  17.             throws AddressException, MessagingException, GeneralSecurityException {
  18.  
  19.         MailSSLSocketFactory sf = new MailSSLSocketFactory();
  20.         sf.setTrustAllHosts(true);
  21.         // 1.创建一个程序与邮件服务器会话对象 Session
  22.         Properties props = new Properties();
  23.         props.setProperty("mail.transport.protocol", "SMTP");
  24. //        props.setProperty("mail.smtp.host", "smtp.163.com");
  25.         props.setProperty("mail.smtp.host", "smtp.qq.com");
  26.         props.setProperty("mail.smtp.port", "465");
  27.         // 指定验证为true
  28.         props.setProperty("mail.smtp.auth", "true");
  29.         props.setProperty("mail.smtp.timeout","2000");
  30.         props.put("mail.smtp.ssl.enable", true);
  31.         props.put("mail.smtp.ssl.socketFactory", sf);
  32.         // 验证账号及密码,密码需要是第三方授权码
  33.         Authenticator auth = new Authenticator() {
  34.             public PasswordAuthentication getPasswordAuthentication() {
  35.                 return new PasswordAuthentication("895771726@qq.com", "****");
  36.             }
  37.         };
  38.         Session session = Session.getInstance(props, auth);
  39.  
  40.         // 2.创建一个Message,它相当于是邮件内容
  41.         Message message = new MimeMessage(session);
  42.         // 设置发送者
  43.         message.setFrom(new InternetAddress("895771726@qq.com"));
  44.         // 设置发送方式与接收者
  45.         message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
  46.         // 设置主题
  47.         message.setSubject("love");
  48.         // 设置内容
  49.         message.setContent(emailMsg, "text/html;charset=utf-8");
  50.  
  51.         // 3.创建 Transport用于将邮件发送
  52.         Transport.send(message);
  53.     }
  54.    
  55.     public static void main(String[] args) throws AddressException, MessagingException, GeneralSecurityException {
  56.          String emailMsg = "验证码:658219";
  57.         sendMail("895771726@qq.com", emailMsg);
  58. //      sendMail("89577126@qq.com", emailMsg);
  59.         }
  60. }
  61.  

Reply to "Mail sending"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website