【问题标题】:How to send java mail in spring mvc如何在spring mvc中发送java邮件
【发布时间】:2017-06-20 10:56:21
【问题描述】:

我有一个 Web 应用程序,我在其中向用户发送邮件。以下是我的代码。

String host = "smtp.gmail.com";
String pwd = "123";
String from = "sender@gmail.com";
String to = "receiver@gmail.com";
String subject = "Test";
String messageText = "This is demo mail";
int port = 587;
boolean sessionDebug = false;

Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));

InternetAddress[] add = {new InternetAddress(to)};  
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

msg.setRecipients(Message.RecipientType.TO, add);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText); //Actual msg

Transport transport = mailSession.getTransport("smtp");
transport.connect(host, from, pwd);   
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

此代码在本地执行,但在服务器域上失败。我进行了很多搜索,但该解决方案对我不起作用。

我尝试了很多,例如将 transport.connect(host, from, pwd); 替换为 transport.connect(host, 587, from, pwd); or 465 以及将 String host="smtp.gmail.com"; 替换为静态域 IP。但仍然无法正常工作。

谁能弄清楚我错过了什么..?

【问题讨论】:

  • 不工作意味着您收到错误消息?如果是这样,添加堆栈跟踪
  • 代码没有错误。该代码在本地运行良好。但在服务器上失败。
  • 这看起来像是 DNS/Route/Firewall 问题,请确保您可以在 Google 邮件服务器上使用 ping 并且 telnet stmp.gmail.com 在您的服务器上也能正常工作。

标签: java spring-mvc jakarta-mail


【解决方案1】:

这是工作示例。 如果这不起作用,那么您的服务器一定有问题..

 public static void sendEmail(String host, String port, final String userName, final String password, String recipient, String subject, String message, File attachFile) throws AddressException, MessagingException, UnsupportedEncodingException {
     // sets SMTP server properties
     try {
      logger.info("Sending Email to : " + recipient + " Subject :" + subject);
      Properties properties = new Properties();
      properties.put("mail.smtp.host", host);
      properties.put("mail.smtp.port", port);
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.user", userName);
      properties.put("mail.password", password);

      // creates a new session with an authenticator
      Authenticator auth = new Authenticator() {
       @Override
       public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
       }
      };
      Session session = Session.getInstance(properties, auth);

      // creates a new e-mail message
      Message msg = new MimeMessage(session);

      msg.setFrom(new InternetAddress(userName, userName));
      if (recipient.contains(";")) {

       String[] recipientList = recipient.split(";");
       InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
       int counter = 0;
       for (String obj: recipientList) {
        recipientAddress[counter] = new InternetAddress(obj.trim());
        counter++;
       }
       msg.setRecipients(Message.RecipientType.TO, recipientAddress);

      } else {
       InternetAddress[] recipientAddress = {
        new InternetAddress(recipient)
       };
       msg.setRecipients(Message.RecipientType.TO, recipientAddress);
      }
      msg.setSubject(subject);
      msg.setSentDate(new Date());

      // creates message part
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setContent(message, "text/html");

      // creates multi-part
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      // adds attachments
      if (attachFile != null) {
       MimeBodyPart attachPart = new MimeBodyPart();

       try {
        attachPart.attachFile(attachFile);
       } catch (IOException ex) {
        ex.printStackTrace();
       }

       multipart.addBodyPart(attachPart);
      }

      // sets the multi-part as e-mail's content
      msg.setContent(multipart);

      // sends the e-mail
      Transport.send(msg);
     } catch (Exception e) {
      logger.error("ERROR In Sending Email :" + e, e);
     }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2014-06-06
    • 2016-11-06
    • 2015-11-07
    • 1970-01-01
    • 2017-09-20
    • 2016-08-21
    相关资源
    最近更新 更多