Šiame nuosekliame vadove aprašoma, kaip siųsti el. laiškus C# naudojant Aspose.Email for .NET. Mes siunčiame el. laišką naudodami Gmail SMTP serverį šioje C# konsolės programoje, bet galite siųsti el. laiškus naudodami bet kurį SMTP serverį bet kokio tipo .NET programose.
Veiksmai, kaip siųsti el. laišką C#
- Nustatykite Aspose.Email for .NET naudodami NuGet paketų tvarkytuvę
- Įtraukite šias vardų sritis: Aspose.Email, Aspose.Email.Clients ir Aspose.Email.Clients.Smtp
- Taikykite licenciją naudodami Aspose.Email.License klasę
- Sukurkite naują el. laišką naudodami MailMessage klasę
- Sukurkite SmtpClient klasės egzempliorių
- Nustatykite Gmail SMTP kliento informaciją, per kurią būtų siunčiamas el
- Siųskite email naudodami SmtpClient klasės siuntimo metodą
Kodas el. paštui siųsti naudojant C#
using System; | |
//Add Aspose.Email for .NET package reference | |
//Use following namespaces to convert OTG to PDF format | |
using Aspose.Email; | |
using Aspose.Email.Clients; | |
using Aspose.Email.Clients.Smtp; | |
namespace SendEmailUsingSMTPServer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before sending email through Gmail SMTP | |
//using Aspose.Email for .NET | |
Aspose.Email.License AsposeEmailLicense = new Aspose.Email.License(); | |
AsposeEmailLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//create an instance of MailMessage | |
MailMessage EmailMessage = new MailMessage(); | |
//Set email message properties which you want to specify | |
EmailMessage.Subject = "How to Send Mail Using SMTP Server in C#"; | |
EmailMessage.To = "ReceiverEmail@EmailServer.com"; | |
EmailMessage.Body = "This is a test of sending email using SMTP in C#."; | |
//Initiate an instance of SmptpClient class | |
SmtpClient SMTPEmailClient = new SmtpClient(); | |
//Set SMTP client properties so the email message can get through the server | |
SMTPEmailClient.Host = "smtp.gmail.com"; | |
SMTPEmailClient.Username = "YourEmail@gmail.com"; | |
SMTPEmailClient.Password = "Your Gamil Password"; | |
SMTPEmailClient.Port = 587; | |
SMTPEmailClient.SecurityOptions = SecurityOptions.SSLExplicit; | |
//Finally send the email message using Gmail's SMTP client | |
SMTPEmailClient.Send(EmailMessage); | |
} | |
} | |
} |
Problema, kai siunčiate el. laišką naudodami Gmail SMTP C#
Galite susidurti su išimtimi, pvz., “SMTP serveriui reikalingas saugus ryšys arba klientas nebuvo autentifikuotas”. Taip yra todėl, kad Gmail mano, kad jūsų programa yra mažiau saugi, todėl gali neleisti siųsti el. laiškų naudojant savo SMTP serverį. Norėdami išspręsti Gmail SMTP problemą allow access to less secure apps to send email.