c# - 通过 C# 应用程序通过 gmail 发送电子邮件
问题描述
我有一个 C# 应用程序,我要在其中发送电子邮件。当我使用我的个人电子邮件发送电子邮件时,该功能工作正常。但是,我创建了一个 gmail 帐户,用于公司发送电子邮件,但它不适用于该电子邮件。在我创建的 gmail 帐户上,我已将访问不太安全的应用程序设置为开启。这与我为使其与我的个人电子邮件一起工作所做的相同。但是,它不适用于创建的新 gmail 帐户。有任何想法吗?
这是代码
public static void SendEmail(string strEmail,string strRandomPassword)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("email@gmail.com");
message.To.Add(new MailAddress(strEmail));
message.Subject = "Password Reset";
message.IsBodyHtml = true; //to make message body as html
message.Body = "Here is your key to reset your password. Key: " + strRandomPassword;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com"; //for gmail host
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email@gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(message + ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
解决方案
更好的选择是使用应用程序密码 您不需要使用安全性较低的应用程序
class Program
{
private const string SmtpServer = "smtp.gmail.com";
private const string MailserverLogin = "ddddd@gmail.com";
private const string MailServerPassword = "wohpkockczwcjznp";
private const string MailUserName = "Developer tips support";
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// SmtpClient
var client = new SmtpClient(SmtpServer)
{
Port = 587,
Credentials = new System.Net.NetworkCredential(MailserverLogin, MailServerPassword),
EnableSsl = true
};
// Specify the email sender.
var from = new MailAddress(MailserverLogin, MailUserName, System.Text.Encoding.UTF8);
// Set destinations for the email message.
var to = new MailAddress("xxxxx@daimto.com");
// Specify the message content.
var message = new MailMessage(@from, to)
{
Body = "This is a test email message sent by an application. ",
Subject = "Customer support Daimto."
};
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
var userState = "test message1";
client.SendAsync(message, userState);
while (!mailSent)
{
Console.Write(".");
Thread.Sleep(50);
}
//clean up.
message.Dispose();
client.Dispose();
}
static bool mailSent = false;
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
var token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
}
else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
}
推荐阅读
- python - Gekko 找不到小问题的解决方案
- javascript - 在这种特殊情况下(通过共享调用),Javascript 会发生什么?
- ios - ASWebAuthenticationSession/SFAuthenticationSession中callbackURLScheme的作用是什么?
- java - java华氏到摄氏度,反之亦然转换器在jframe中
- web - 我将tailwindCSS 用于一个简单的html 站点,但在netlify 上部署不起作用,为什么?
- powershell - Powershell 更改文本
- sql - 在这样的表 R(ABCDE) 上,我需要一个 SQL 触发器,以防止元组的 A 和 B 列相同但尝试为 C 赋予不同的值
- css - 创建三个单独按钮的垂直堆栈
- ios - 包含静态预填充和动态用户数据的核心数据
- python - 无法在 Python 中导入名称模型