我在考虑使用我的Gmail账户发送电子邮件信息,而不是依赖于我的主机来发送电子邮件。这些电子邮件是针对我在节目中播放的乐队的个性化邮件。
这样做是否可能?
我在考虑使用我的Gmail账户发送电子邮件信息,而不是依赖于我的主机来发送电子邮件。这些电子邮件是针对我在节目中播放的乐队的个性化邮件。
这样做是否可能?
请务必使用System.Net.Mail
而不是已弃用的System.Web.Mail
。在System.Web.Mail
中使用SSL会使其变得混乱且充斥着hacky扩展。
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
此外,请前往Google帐户>安全页面,查看登录Google> 2步验证设置。
fromPassword
常量中使用生成的密码,而不是您的标准Gmail密码。smtp.gmail.com
不同的东西)并使用something@gmail.com
作为发件人,则可能会遇到问题。顺便说一下:如果发件人地址不是您的,smtp.gmail.com
会自动覆盖它。 - Meinersbur以上答案不起作用。您必须设置DeliveryMethod = SmtpDeliveryMethod.Network
,否则它会返回一个"客户端未经身份验证"的错误。此外,设置超时时间总是个好主意。
修改后的代码:
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年修改: 自2022年5月30日起,谷歌将不再支持使用仅使用用户名和密码登录您的Google账户的第三方应用程序或设备。 但您仍然可以通过Gmail账户发送电子邮件。
现在只需使用此密码替换原始密码即可!
public static void SendMail2Step(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body, string[] FileNames) {
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);
}
使用方法如下:
SendMail2Step("smtp.gmail.com", 587, "youraccount@gmail.com",
"yjkjcipfdfkytgqv",//This will be generated by google, copy it here.
"recipient@barcodes.bg", "test message subject", "Test message body ...", null);
为了让其他答案“从服务器”起作用,您需要在gmail帐户中启用不安全应用程序的访问权限。这将于2022年5月30日停用。
看起来谷歌最近更改了其安全策略。排名靠前的答案不再适用,除非您按照此处描述的更改帐户设置:https://support.google.com/accounts/answer/6010255?hl=zh-CN 截至2016年3月,谷歌再次更改了设置位置!
这是用附件发送电子邮件的简单方法。
来源:http://coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your mail@gmail.com");
mail.To.Add("to_mail@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
Google可能会阻止一些不使用现代安全标准的应用程序或设备的登录尝试。由于这些应用程序和设备更容易遭受攻击,阻止它们有助于保护您的账户安全。
一些不支持最新安全标准的应用程序的示例包括:
因此,您需要在Google账户中启用较不安全的登录。
登录Google账户后,转到:
https://myaccount.google.com/lesssecureapps
或
https://www.google.com/settings/security/lesssecureapps
在C#中,您可以使用以下代码:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("email@gmail.com");
mail.To.Add("somebody@domain.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
为了让它正常工作,我必须启用我的 Gmail 帐户,使其他应用程序能够访问。这可以通过 "启用不安全的应用程序" 和 使用此链接 来完成: https://accounts.google.com/b/0/DisplayUnlockCaptcha
以下是我的版本:"使用 Gmail 在 C# 中发送电子邮件"。
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();
}
}
}
}
我希望这段代码能够正常运行,你可以试一试。
// Include this.
using System.Net.Mail;
string fromAddress = "xyz@gmail.com";
string mailPassword = "*****"; // Mail id password from where mail will be sent.
string messageBody = "Write the body of the message here.";
// Create smtp connection.
SmtpClient client = new SmtpClient();
client.Port = 587;//outgoing port for the mail.
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);
// Fill the mail form.
var send_mail = new MailMessage();
send_mail.IsBodyHtml = true;
//address from where mail will be sent.
send_mail.From = new MailAddress("from@gmail.com");
//address to which mail will be sent.
send_mail.To.Add(new MailAddress("to@example.com");
//subject of the mail.
send_mail.Subject = "put any subject here";
send_mail.Body = messageBody;
client.Send(send_mail);
以下是使用C#发送电子邮件的示例代码,此示例代码演示如何使用Google的SMTP服务器发送电子邮件。
代码相当简单易懂,请将您的电子邮件和密码值替换为代码中的email和password变量。
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);
}