前提・実現したいこと
C#でフィードバックを自分のYahooメールに送信するプログラムを試作しています。
発生している問題・エラーメッセージ
送信元のメールアドレス、タイトル及び内容を入力し
OKをクリックするとSMTP.Send(MailInfo);の行で例外「System.Net.Mail.SmtpException」が発生します。
一体どこが悪いのかご教授お願いします。
メールの送信に失敗しました。 System.Net.Mail.SmtpException: SMTP サーバーにセキュリティで保護された接続が必要であるか、またはクライアントが認証されていません。 サーバーの応答:Sorry, please use SMTP-AUTH instead - for help go to https://www.yahoo-help.jp/app/answers/detail/a_id/79799/p/622 場所 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 場所 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) 場所 System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) 場所 System.Net.Mail.SmtpClient.Send(MailMessage message) 場所 WindowsFormsApp18.feedback.OK_Click(Object sender, EventArgs e) 場所 C:\Users***\source\repos\WindowsFormsApp18\WindowsFormsApp18\feedback.cs:行 75
該当のソースコード
feedback.cs
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Windows.Forms; 9using System.Net.Mail; 10 11namespace WindowsFormsApp18 12{ 13 public partial class feedback : Form 14 { 15 16 string[] BadWord = { "***","***","***" }; 17 18 public feedback() 19 { 20 InitializeComponent(); 21 } 22 23 private void feedback_Load(object sender, EventArgs e) 24 { 25 26 } 27 28 private void OK_Click(object sender, EventArgs e) 29 { 30 ErrorProvider1.Clear(); 31 if (MailAdress.Text == "") 32 { 33 MessageBox.Show("送信元のメールアドレスが入力されていません。", Form2.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 34 ErrorProvider1.SetError(MailAdress, "送信元のメールアドレスが入力されていません。"); 35 goto MailSkip; 36 } 37 else if (MailTitle.Text == "") 38 { 39 MessageBox.Show("フィードバックのタイトルが入力されていません。", Form2.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 40 ErrorProvider1.SetError(MailTitle, "フィードバックのタイトルが入力されていません。"); 41 goto MailSkip; 42 } 43 else if (Message.Text == "") 44 { 45 MessageBox.Show("フィードバックの内容が入力されていません。", Form2.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 46 goto MailSkip; 47 } 48 49 for (int i = 0; i < BadWord.Length; i++) 50 { 51 if (Message.Text.Contains(BadWord[i]) == true || MailTitle.Text.Contains(BadWord[i]) == true) 52 { 53 MessageBox.Show("フィードバックのタイトルまたは内容に不適切な表現が含まれています。", Form2.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 54 goto MailSkip; 55 } 56 } 57 try 58 { 59 MailAddress ToMailAddress = new MailAddress(MailAdress.Text); 60 MailAddress FromMailAddress = new MailAddress("xxxxx@yahoo.co.jp"); 61 MailMessage MailInfo = new MailMessage(); 62 System.Net.NetworkCredential Certification = new System.Net.NetworkCredential("YahooID", "Password"); 63 MailInfo.To.Add(ToMailAddress); 64 MailInfo.From = FromMailAddress; 65 MailInfo.Subject = MailTitle.Text; 66 MailInfo.Body = Message.Text; 67 MailInfo.BodyEncoding = System.Text.Encoding.GetEncoding("iso-2022-jp"); 68 SmtpClient SMTP = new SmtpClient(); 69 SMTP.Port = 587; 70 SMTP.DeliveryMethod = SmtpDeliveryMethod.Network; 71 SMTP.Host = "smtp.mail.yahoo.co.jp"; 72 SMTP.Credentials = Certification; 73 SMTP.EnableSsl = false; 74 SMTP.Send(MailInfo); 75 MailInfo.Dispose(); 76 SMTP.Dispose(); 77 } 78 catch (Exception err) 79 { 80 MessageBox.Show("メールの送信に失敗しました。\n\n" + err, Form2.AppName, MessageBoxButtons.OK, MessageBoxIcon.Hand); 81 } 82 MailSkip:; 83 } 84 85 private void Cansel_Click(object sender, EventArgs e) 86 { 87 88 } 89 } 90} 91
試したこと
SMTP.Port = 587;をSMTP.Port = 465;、SMTP.EnableSsl = false;をSMTP.EnableSsl = true;に変えたりしましたが問題は解決しませんでした。
補足情報(FW/ツールのバージョンなど)
.NET Framework 4.0
Microsoft Visual Studio 2017 Community
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。