2016-06-29 7 views
-2

Javaアプリケーションを使用して電子メールを送信したいと思います。 ボタンを押すと、自動的に電子メールが送信されるはずですが、何とか解決策がまだ見つかりませんでした。 インターネットで多くのサンプルコードを見つけましたが、Gmail/gmxやOutlookを使用しても問題はありません。メッセージは常に表示されます:Javaメールを使用する

"SMTPホストに接続できませんでした:mail.gmx.net 、ポート:587; ネストされた例外は次のとおりです。 java.net.ConnectException:接続がタイムアウト:ドメインに基づく」

を接続し、そのホストがmail.gmx.netまたはsmtp.office365.comなど..です どうやら接続の問題があると思いますが、修正できませんでした。 あなたに適したアイデアやコードがありますか?

ありがとうございます。

トビアス

+0

ようこそ正常に動作します! [MCVE]を投稿する方法を見てください。 – Frank

+0

'Connection timed out/connect'は通常、ファイアウォールの問題を示します。 – EJP

答えて

-1

使用送信メール.Thisのため、このコードは、スタックオーバーフローの詳細情報については

package SendMail; 

import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

/** 
* @author akash073 
* 
*/ 

public class CrunchifyJavaMailExample { 

    //static Properties mailServerProperties; 
    // static Session getMailSession; 
    // static MimeMessage generateMailMessage; 

    public static void main(String args[]) throws AddressException, MessagingException { 
     generateAndSendEmail(); 
     System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email.."); 
    } 

    public static void generateAndSendEmail() throws AddressException, MessagingException { 

     String smtpHost="put Your Host"; 
     String smtpUser="UserName in full @somthing.com"; 
     String smtpPassword="your password"; 
     int smtpPort=25;//Port may vary.Check yours smtp port 
     // Step1 
     System.out.println("\n 1st ===> setup Mail Server Properties.."); 
     Properties mailServerProperties = System.getProperties(); 
     //mailServerProperties.put("mail.smtp.ssl.trust", smtpHost); 
//  mailServerProperties.put("mail.smtp.starttls.enable", true); // added this line 
     mailServerProperties.put("mail.smtp.host", smtpHost); 
     mailServerProperties.put("mail.smtp.user", smtpUser); 
     mailServerProperties.put("mail.smtp.password", smtpPassword); 
     mailServerProperties.put("mail.smtp.port", smtpPort); 

     mailServerProperties.put("mail.smtp.starttls.enable", "true"); 
     System.out.println("Mail Server Properties have been setup successfully.."); 

     // Step2 
     System.out.println("\n\n 2nd ===> get Mail Session.."); 
     Session getMailSession = Session.getDefaultInstance(mailServerProperties, null); 
     MimeMessage generateMailMessage = new MimeMessage(getMailSession); 
     generateMailMessage.setFrom (new InternetAddress (smtpUser)); 
     generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); 
     generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("[email protected]")); 
     generateMailMessage.setSubject("Greetings from Crunchify.."); 
     String emailBody = "2.Test email by Crunchify.com JavaMail API example. " + "<br><br> Regards, <br>Crunchify Admin"; 
     generateMailMessage.setContent(emailBody, "text/html"); 
     System.out.println("Mail Session has been created successfully.."); 

     // Step3 
     System.out.println("\n\n 3rd ===> Get Session and Send mail"); 
     Transport transport = getMailSession.getTransport("smtp"); 

     // Enter your correct gmail UserID and Password 
     // if you have 2FA enabled then provide App Specific Password 
     transport.connect(smtpHost,smtpPort, smtpUser, smtpPassword); 
     transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients()); 
     transport.close(); 
    } 
} 

ため crunchify

+0

OPで報告された実際の問題を解決するものはありません。 – EJP

関連する問題