有些 SMTP 服务器要求在代表客户端发送电子邮件前验证客户端的身份。当此 SmtpClient 对象应该使用当前登录用户的默认凭据进行身份验证(如果服务器要求)
有些 SMTP 服务器要求在代表客户端发送电子邮件前验证客户端的身份。当此 SmtpClient 对象应该使用当前登录用户的默认凭据进行身份验证(如果服务器要求)时,请将此属性设置为 true。对于客户端应用程序,这是在大多数情况下都需要的行为。也可以使用应用程序和计算机配置文件指定凭据信息。有关更多信息,请参见 <mailSettings> 元素(网络设置)。
如果 UseDefaultCredentials 属性设置为 false,则连接到服务器时会将 Credentials 属性中设置的值用作凭据。如果 UseDefaultCredentials 属性设置为 false 并且尚未设置 Credentials 属性,则将邮件以匿名方式发送到服务器。UseDefaultCredentials 的默认值为false。
如果提供用于基本身份验证的凭据,则凭据将以明文形式发送到服务器。这可能会产生安全问题,因为凭据可以被他人看见从而加以利用。但是向163的邮箱发送邮件是必须要带上发送者的凭据信息,否则邮件是发送不出去的。
SmtpClient也可以这样建立:SmtpClient client=new SmtpClient(smtp_server)
,因为邮件发送服务器的默认端口是25。
private static void TimeEvent(object source, ElapsedEventArgs e)
{
string smtp_server="smtp.qq.com";
int port = 25;
string mail_from = "***from_mail_username***@qq.com";
string sender = "***from_mail_username***";
string mail_to = "***to_mail_username***@163.com";
string receiver = "***to_mail_username***@";
string subject = "Title Test"
string body = "Content Test";
try
{
SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
MailAddress from = new MailAddress(mail_from, sender);
MailAddress to = new MailAddress(mail_to, receiver);
MailMessage message = new MailMessage(from, to);
message.BodyEncoding = Encoding.UTF8;
message.IsBodyhtml = true;
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient(smtp_server, port);
client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***");
client.Send(message);
}
private static void TimeEvent(object source, ElapsedEventArgs e)
{
string smtp_server="smtp.163.com";
int port = 25;
string mail_from = "***from_mail_username***@163.com";
string sender = "***from_mail_username***";
string mail_to = "***to_mail_username***@163.com";
string receiver = "***to_mail_username***";
string subject = "Title Test"
string body = "Content Test";
try
{
SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
MailAddress from = new MailAddress(mail_from, sender);
MailAddress to = new MailAddress(mail_to, receiver);
MailMessage message = new MailMessage(from, to);
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient(smtp_server);
client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***");
client.Send(message);
}
private static void TimeEvent(object source, ElapsedEventArgs e)
{
string smtp_server="smtp.163.com";
int port = 25;
string mail_from = "***from_mail_username***@163.com";
string sender = "***from_mail_username***";
string mail_to = "***to_mail_username***@qq.com";
string receiver = "***to_mail_username***";
string subject = "Title Test"
string body = "Content Test";
try
{
SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
MailAddress from = new MailAddress(mail_from, sender);
MailAddress to = new MailAddress(mail_to, receiver);
MailMessage message = new MailMessage(from, to);
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient(smtp_server);
client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***");
client.Send(message);
}
private static void TimeEvent(object source, ElapsedEventArgs e)
{
string smtp_server="smtp.qq.com";
int port = 25;
string mail_from = "***from_mail_username***@qq.com";
string sender = "***from_mail_username***";
string mail_to = "***to_mail_username***@qq.com";
string receiver = "***to_mail_username***";
string subject = "Title Test"
string body = "Content Test";
try
{
SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
MailAddress from = new MailAddress(mail_from, sender);
MailAddress to = new MailAddress(mail_to, receiver);
MailMessage message = new MailMessage(from, to);
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient(smtp_server);
client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***");
client.Send(message);
}
到此这篇关于C#使用System.net.Mail类实现邮件发送的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。
--结束END--
本文标题: C#使用System.Net.Mail类实现邮件发送
本文链接: https://lsjlt.com/news/154154.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0