Dom*_*nic 1042
一定要使用System.Net.Mail,而不是弃用System.Web.Mail.使用SSL System.Web.Mail是一个混乱的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);
}
Run Code Online (Sandbox Code Playgroud)
Don*_* V. 150
以上答案不起作用.您必须设置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);
}
Run Code Online (Sandbox Code Playgroud)
小智 93
对于"从服务器"工作的其他答案,首先打开访问权限以获取 Gmail帐户中安全性较低的应用程序.
看起来最近谷歌改变了它的安全政策.评分最高的答案不再有效,直到您按照此处所述更改帐户设置:https://support.google.com/accounts/answer/6010255?hl = zh-CN
截至2016年3月,谷歌再次更改了设置位置!
Ran*_*ddy 39
这是发送附带附件的电子邮件..简单而简短..
来源: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);
}
Run Code Online (Sandbox Code Playgroud)
mjb*_*mjb 18
Google可能阻止某些不使用现代安全标准的应用或设备尝试登录.由于这些应用和设备更易于破解,因此阻止它们有助于确保您的帐户更安全.
一些不支持最新安全标准的应用程序示例包括:
因此,您必须在Google帐户中启用Less Secure Sign-In.
登录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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 15
这是我的版本:" 使用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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ans 14
为了让我能够工作,我必须启用我的Gmail帐户才能让其他应用程序获得访问权限.这与"启用安全性较低的应用程序"进行,也通过此链接: https://accounts.google.com/b/0/DisplayUnlockCaptcha
小智 13
我希望这段代码能正常工作.你可以尝试一下.
// 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);
Run Code Online (Sandbox Code Playgroud)
包括这个,
using System.Net.Mail;
Run Code Online (Sandbox Code Playgroud)
然后,
MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt16("587");
client.Credentials = new System.Net.NetworkCredential("mail-id@gmail.com","password");
client.EnableSsl = true;
client.Send(sendmsg);
Run Code Online (Sandbox Code Playgroud)
下面是使用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);
}
Run Code Online (Sandbox Code Playgroud)
从 2022 年 6 月 1 日起,\xe2\x80\x8b\xe2\x80\x8bGoogle 添加了一些安全功能
\nGoogle 不再支持使用要求您仅使用用户名和密码登录 Google 帐户或直接使用 Google 帐户的用户名和密码发送邮件的第三方应用程序或设备。但您仍然可以使用生成应用程序密码通过您的 Gmail 帐户发送电子邮件。
\n\n\n以下是生成新密码的步骤。
\n
现在我们必须使用此密码来发送邮件,而不是您帐户的原始密码。
\n\n\n下面是发送邮件的示例代码
\n
public static void SendMailFromApp(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body) { \n var smtpClient = new SmtpClient(SMTPServer, SMTP_Port) {\n DeliveryMethod = SmtpDeliveryMethod.Network,\n UseDefaultCredentials = false,\n EnableSsl = true\n }; \n smtpClient.Credentials = new NetworkCredential(From, Password); //Use the new password, generated from google!\n var message = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress(From, "SendMail2Step"), new System.Net.Mail.MailAddress(To, To));\n smtpClient.Send(message);\n }\nRun Code Online (Sandbox Code Playgroud)\n您可以像下面这样调用方法
\nSendMailFromApp("smtp.gmail.com", 25, "mygmailaccount@gmail.com",\n "tyugyyj1556jhghg",//This will be generated by google, copy it here.\n "mailme@gmail.com", "New Mail Subject", "Body of mail from My App");\nRun Code Online (Sandbox Code Playgroud)\n
小智 6
如果您想发送背景电子邮件,请执行以下操作
public void SendEmail(string address, string subject, string message)
{
Thread threadSendMails;
threadSendMails = new Thread(delegate()
{
//Place your Code here
});
threadSendMails.IsBackground = true;
threadSendMails.Start();
}
Run Code Online (Sandbox Code Playgroud)
并添加命名空间
using System.Threading;
Run Code Online (Sandbox Code Playgroud)
尝试这个,
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
更改 Gmail / Outlook.com 电子邮件的发件人:
为了防止欺骗 - Gmail/Outlook.com 不允许您从任意用户帐户名发送邮件。
如果您的发件人数量有限,您可以按照以下说明操作,然后将该From字段设置为此地址:从其他地址发送邮件
如果您想从任意电子邮件地址(例如用户输入电子邮件的网站上的反馈表,并且您不希望他们直接向您发送电子邮件)发送邮件,那么您能做的最好的事情是:
msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
Run Code Online (Sandbox Code Playgroud)
这样,您只需在电子邮件帐户中点击“回复”即可在反馈页面上回复乐队的粉丝,但他们不会收到您的实际电子邮件,这可能会导致大量垃圾邮件。
如果您处于受控环境中,这效果很好,但请注意,我看到一些电子邮件客户端即使指定了回复(我不知道是哪一个)也会发送到发件人地址。
用这种方式
MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt32("587");
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("mail-id@gmail.com","MyPassWord");
client.Send(sendmsg);
Run Code Online (Sandbox Code Playgroud)
不要忘记这一点:
using System.Net;
using System.Net.Mail;
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
527174 次 |
| 最近记录: |