###前提・実現したいこと
①javaを使ってメール送信を行いたいです。
しかしsmtp.hostとsmtp.portの設定の仕方が分からないです。
(既定で使用しているメールソフトは無いです。いつもブラウザでoffice365のアカウントにログインし、メールしています。なので、webで調べたoffice365のsmtpサーバー名を入れて実行しました。結果、駄目でした。)
②portについてですが、そもそもportはsmtpサーバのポートで
あって、自分のPCのポートのことではないのでしょうか?
(最初の頃はコマンドプロンプトを開いてnetstat-aで使用しているportを調べ、まだ使われていないportを把握してセットを行いました。しかし、途中で違うのではないかと考え、office365のポートをセットしました。)
④smtpサーバは自分のパソコン内にある訳ではないのでしょうか。
###発生している問題・エラーメッセージ
Exception in thread "main" javax.mail.MessagingException: Unknown SMTP host: smtp.office365.com; nested exception is: java.net.UnknownHostException: smtp.office365.com at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1970) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at softbank_simulator.Send_Mail.send(Send_Mail.java:77) at softbank_simulator.Send_Mail.main(Send_Mail.java:28) Caused by: java.net.UnknownHostException: smtp.office365.com at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:317) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938) ... 8 more
###該当のソースコード
java
1 2package softbank_simulator; 3 4import java.util.Calendar; 5import java.util.Properties; 6//import java.net.PasswordAuthentication; 7 8import javax.mail.Authenticator; 9import javax.mail.Message; 10import javax.mail.PasswordAuthentication; 11import javax.mail.Session; 12import javax.mail.Transport; 13import javax.mail.internet.InternetAddress; 14import javax.mail.internet.MimeMessage; 15 16public class Send_Mail { 17 18 /** 19 * 20 */ 21 public Send_Mail() { 22 // TODO 自動生成されたコンストラクター・スタブ 23 } 24 25 public static void main(String[] args) throws Exception { 26 27 send(); 28 } 29 30 public static void send() throws Exception { 31 Properties props = new Properties(); 32 33 // ServerSocket serversocket = new ServerSocket(9999); 34 // Socket socket = serversocket.accept(); 35 // Socket socket = new Socket ("localhost",9999); 36 // byte [] senddata = { 37 38 props.setProperty("mail.smtp.host", " smtp.office365.com"); 39 props.setProperty("mail.smtp.port", "587"); 40 props.setProperty("mail.smtp.auth", "true"); 41 props.setProperty("mail.debug", "true"); 42 43 props.setProperty("mail.smtp.connectiontimeout", "5000"); 44 props.setProperty("mail.smtp.timeout", "5000"); 45 46 class PasswordAuthenticator extends Authenticator { 47 48 private String username; 49 private String password; 50 51 PasswordAuthenticator(String username, String password) { 52 53 this.username = username; 54 this.password = password; 55 56 } 57 58 public PasswordAuthentication getPasswordAuthentication() { 59 60 return new PasswordAuthentication(username, password); 61 62 } 63 } 64 65 Session session = Session.getInstance(props, new PasswordAuthenticator("aaa","sss"); 66 MimeMessage msg = new MimeMessage(session); 67 68 msg.setRecipient(Message.RecipientType.TO, new InternetAddress("aaa@syahoo.co.jp")); 69 msg.setFrom(new InternetAddress("aa@yahoo.co.jp")); 70 msg.setSubject("JAVAメール"); 71 msg.setText("テストメールです"); 72 73 Calendar c = Calendar.getInstance(); 74 msg.setSentDate(c.getTime()); 75 76 Transport.send(msg); 77 78 } 79 80} 81
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/29 07:23
2017/06/29 07:35