我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
当前回答
这是不再支持,如果你正在尝试这样做现在。
https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637960864118404117-800836189&p=less-secure-apps&rd=1#zippy=
其他回答
using System;
using System.Net;
using System.Net.Mail;
namespace SendMailViaGmail
{
class Program
{
static void Main(string[] args)
{
//Specify senders gmail address
string SendersAddress = "Sendersaddress@gmail.com";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "ReceiversAddress@yahoo.com";
//Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
const string SendersPassword = "Password";
//Write the subject of ur mail
const string subject = "Testing";
//Write the contents of your mail
const string body = "Hi This Is my Mail From Gmail";
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
来源:在ASP发送电子邮件。净c#
下面是一个使用c#发送邮件的示例工作代码,在下面的例子中,我使用谷歌的smtp服务器。
代码是相当不言自明的,替换电子邮件和密码与您的电子邮件和密码值。
public void SendEmail(string address, string subject, string message)
{
string email = "yrshaikh.mail@gmail.com";
string password = "put-your-GMAIL-password-here";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
这是不再支持,如果你正在尝试这样做现在。
https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637960864118404117-800836189&p=less-secure-apps&rd=1#zippy=
上面的答案不管用。必须设置DeliveryMethod = SmtpDeliveryMethod。网络,否则将返回“客户端未经过身份验证”错误。此外,暂停总是一个好主意。
修改后的代码:
using System.Net.Mail;
using System.Net;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@yahoo.com", "To Name");
const string fromPassword = "password";
const string subject = "test";
const string body = "Hey now!!";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
从2022年6月1日起,谷歌增加了一些安全功能
谷歌不再支持使用仅要求您使用您的用户名和密码登录您的谷歌帐户或直接使用谷歌帐户的用户名和密码发送邮件的第三方应用程序或设备。但是您仍然可以通过您的gmail帐户使用生成应用程序密码发送电子邮件。
以下是生成新密码的步骤。
访问https://myaccount.google.com/security 打开两步验证。 如有需要,请电话确认。 点击“应用程序密码”,就在“2步验证”勾的下方。请求邮件应用程序的新密码。
现在我们要用这个密码来发邮件,而不是你原来的账号密码。
下面是发送邮件的示例代码
public static void SendMailFromApp(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body) {
var smtpClient = new SmtpClient(SMTPServer, SMTP_Port) {
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = true
};
smtpClient.Credentials = new NetworkCredential(From, Password); //Use the new password, generated from google!
var message = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress(From, "SendMail2Step"), new System.Net.Mail.MailAddress(To, To));
smtpClient.Send(message);
}
你可以像下面这样调用方法
SendMailFromApp("smtp.gmail.com", 25, "mygmailaccount@gmail.com",
"tyugyyj1556jhghg",//This will be generated by google, copy it here.
"mailme@gmail.com", "New Mail Subject", "Body of mail from My App");