2017-06-20 44 views
-2

我有一個web應用程序,我正在向用戶發送郵件。以下是我的代碼。如何在春天發送java郵件mvc

String host = "smtp.gmail.com"; 
String pwd = "123"; 
String from = "[email protected]"; 
String to = "[email protected]"; 
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 465String host="smtp.gmail.com";與靜態域IP。但仍然無法正常工作。

任何人都可以找出我失蹤的東西..?

+2

*不工作*表示您收到錯誤信息?如果是這樣,請添加堆棧跟蹤 – Jens

+0

代碼中沒有錯誤。該代碼在當地很有用。但在服務器上失敗。 – Ashish

+0

它看起來像一個DNS /路由/防火牆問題,確保你可以'ping'谷歌郵件服務器和'telnet stmp.gmail.com'也可以從你的服務器上運行。 –

回答

0

這裏是工作示例。 如果這不起作用,那麼您的服務器肯定有問題。

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); 
    } 
    }