使用C#语言发送电子邮件的简单代码 [英] Simple code for send email with C# language
本文介绍了使用C#语言发送电子邮件的简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I wanted to create a simple application to send an email with c # .net language .
any someone can help me . . ?
I need a simple code examples
please help me.
我尝试了什么:
i需要使用c#语言发送电子邮件的示例代码
What I have tried:
i need sample code to send email with c# language
推荐答案
请看这里:使用或不使用附件在C#中发送电子邮件:通用例程。 [ ^ ]
我写了一篇文章,内容涵盖了使用C#语言在.NET框架中发送电子邮件的整个概念。你可以在这里阅读它,通过.NET框架发送电子邮件,和一般问题 - 使用C#代码 [ ^ ]。
我还包括了编程发送电子邮件的应用程序时可能遇到的一些错误和问题。使用C#发送电子邮件的常规代码是:
I wrote an article covering the entire concept of sending the emails in .NET framework using C# language. You can read about it here, Sending emails over .NET framework, and general problems – using C# code[^].
I also included a few of the errors and problems that you might encounter while programming the application for sending the emails. General code for sending the emails in C# is:
// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
// Configure the client
client.EnableSsl = true;
client.Credentials = new NetworkCredential("<username>", "<password>");
// client.UseDefaultCredentials = true;
// A client has been created, now you need to create a MailMessage object
MailMessage message = new MailMessage(
"from@example.com", // From field
"to@example.com", // Recipient field
"Hello", // Subject of the email message
"World!" // Email message body
);
// Send the message
client.Send(message);
/*
* Since I was using Console app, that is why I am able to use the Console
* object, your framework would have different ones.
* There is actually no need for these following lines, you can ignore them
* if you want to. SMTP protocol would still send the email of yours. */
// Print a notification message
Console.WriteLine("Email has been sent.");
// Just for the sake of pausing the application
Console.Read();
}
您可以将其用作帮助函数左右。更新值,如果一切顺利,代码就可以了!
You can use this as a helper function or so. Update the values and if everything goes fine, the code would work!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("xxxxx@gmail.com");
mail.To.Add("yyyyyy@yahoo.com");
mail.Subject = "Halo Boss";
mail.Body = "Halo Boss, Super";
mail.Priority = MailPriority.Normal;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("D:\\Default.aspx");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("xxxxxx@gmail.com", "1234");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Console.Write("Email Terkirim");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
i try do like above. but when i run, i got the message error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at.
and then i also got email from gmail that related security system.
so, what can i do for it. please help me :'(
这篇关于使用C#语言发送电子邮件的简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文