2017-06-20 14 views
-2

私はユーザーにメールを送信しているWebアプリケーションがあります。以下は私のコードです。春のMVCでJavaメールを送信するには

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 465と置き換えてみました。また、String host="smtp.gmail.com";を静的ドメインIPに置き換えました。まだ動作していません。

誰でも私が迷っているものを理解できますか?

+2

は* *動作しないことは、あなたがエラーメッセージが表示されます意味?その場合は、スタックトレースを追加してください。 – Jens

+0

コードにエラーはありません。このコードはローカルでうまく機能します。サーバーでは失敗します。 – Ashish

+0

DNS /ルート/ファイアウォールの問題のように見えますが、Googleのメールサーバーに '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); 
    } 
    } 
関連する問題