Место общения программистов C#
Anonymous
Как правильно отправить электронное письмо с помощью System.Net.Mail
Сообщение
Anonymous » 24 сен 2024, 18:39
Я работал в Visual Studio с Windows Forms. Я добавил метод SendVerificationCode для отправки кода подтверждения с помощью System.Net.Mail:
Код: Выделить всё
using System.Net.Mail;
namespace PasswordManager
{
public partial class Form1 : Form
{
Random rnd = new();
private void SendVerificationCode(object sender, EventArgs e)
{
if (!(textBox4.Text == "") && !(textBox2.Text == ""))
{
verificationCode = "";
for (int _ = 0; _ < 4; _++)
{
verificationCode += nums[rnd.Next(0, 10)];
}
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
mail.From = new MailAddress("**********@gmail.com");
mail.To.Add(textBox2.Text);
mail.Subject = "Verification code";
mail.Body = verificationCode;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("*********@gmail.com", "**********");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception)
{
throw new Exception("It's not working");
}
}
}
}
}
TextBox2.Text был ********@wp.pl.
Он выдает исключение «это не работает".
Может ли кто-нибудь мне помочь?
Спасибо.
Подробнее здесь:
https://stackoverflow.com/questions/790 ... m-net-mail
1727192342
Anonymous
Я работал в Visual Studio с Windows Forms. Я добавил метод SendVerificationCode для отправки кода подтверждения с помощью System.Net.Mail: [code]using System.Net.Mail; namespace PasswordManager { public partial class Form1 : Form { Random rnd = new(); private void SendVerificationCode(object sender, EventArgs e) { if (!(textBox4.Text == "") && !(textBox2.Text == "")) { verificationCode = ""; for (int _ = 0; _ < 4; _++) { verificationCode += nums[rnd.Next(0, 10)]; } try { MailMessage mail = new MailMessage(); SmtpClient smtp = new SmtpClient(); mail.From = new MailAddress("**********@gmail.com"); mail.To.Add(textBox2.Text); mail.Subject = "Verification code"; mail.Body = verificationCode; smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential("*********@gmail.com", "**********"); smtp.EnableSsl = true; smtp.Send(mail); } catch (Exception) { throw new Exception("It's not working"); } } } } } [/code] TextBox2.Text был ********@wp.pl. Он выдает исключение «это не работает". Может ли кто-нибудь мне помочь? Спасибо. Подробнее здесь: [url]https://stackoverflow.com/questions/79019234/how-to-send-correctly-an-email-using-system-net-mail[/url]