###前提・実現したいこと
http://d.hatena.ne.jp/maji-KY/20111001/1317483898#c
このHPのソースを元に自分で
javaでメール送信のプログラムを作成したいのですが、色々とエラーが出てしまいましたので、
どうしてこうなったか、どうしたらよいのか教えてください。
(ちなみにHPのソースコードをそのままコピーしてeclipseに貼り付けたら何もエラーが
出なかったので、HPの内容は問題ないと思われます。)
###発生している問題・エラーメッセージ
①public PasswordAuthentication getPasswordAuthentication() { →PasswordAuthenticationにて赤線(エラー)、 戻りの型は Authenticator.getPasswordAuthentication() と互換性がありませんとのこと。 ②Session session = Session.getInstance(props, new PasswordAuthenticator("m", "m")); →getInstanceにて赤線(エラー)、 型 Session のメソッド getInstance(Properties, Authenticator) は引数 (Properties, PasswordAuthenticator) に適用できませんとのこと
###該当のソースコード
java
1 2package softbank_simulator; 3 4import java.net.Authenticator; 5import java.util.Calendar; 6import java.util.Properties; 7//import java.net.PasswordAuthentication; 8 9import javax.mail.Message; 10import javax.mail.Session; 11import javax.mail.Transport; 12import javax.mail.internet.InternetAddress; 13import javax.mail.internet.MimeMessage; 14import javax.mail.PasswordAuthentication; 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 props.setProperty("mail.smtp.host", " smtp.office365.com"); 33 props.setProperty("mail.smtp.port", "587"); 34 props.setProperty("mail.smtp.auth", "true"); 35 props.setProperty("mail.debug", "true"); 36 37 props.setProperty("mail.smtp.connectiontimeout", "5000"); 38 props.setProperty("mail.smtp.timeout", "5000"); 39 40 class PasswordAuthenticator extends Authenticator { 41 42 private String username; 43 private char[] password; 44 45 PasswordAuthenticator(String username, String password) { 46 47 this.username = username; 48 this.password = password; 49 50 } 51 52 public PasswordAuthentication getPasswordAuthentication() { 53 54 return new PasswordAuthentication(username, password); 55 56 } 57 } 58 59 Session session = Session.getInstance(props, new PasswordAuthenticator("m", "m")); 60 MimeMessage msg = new MimeMessage(session); 61 62 msg.setRecipient(Message.RecipientType.TO, new InternetAddress("aaa@hhh.co.jp")); 63 msg.setFrom(new InternetAddress("aaa@hhh.co.jp")); 64 msg.setSubject("JAVAメール"); 65 msg.setText("テストメールです"); 66 67 Calendar c = Calendar.getInstance(); 68 msg.setSentDate(c.getTime()); 69 70 Transport.send(msg); 71 72 } 73 74} 75 76
###補足情報(言語/FW/ツール等のバージョンなど)
java1.5 SE
eclipse
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/29 05:29