Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- {
- using (var mail = new MailMessage())
- {
- const string email = "*******@gmail.com";
- const string password = "*********";
- var loginInfo = new NetworkCredential(email, password);
- mail.From = new MailAddress("*******@gmail.com");
- mail.To.Add(new MailAddress("email@email.com"));
- mail.Subject = subject;
- mail.Body = body;
- mail.IsBodyHtml = true;
- try
- {
- using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
- {
- smtpClient.EnableSsl = true;
- smtpClient.UseDefaultCredentials = false;
- smtpClient.Credentials = loginInfo;
- smtpClient.Send(mail);
- }
- }
- finally
- {
- mail.Dispose();
- }
- }
- }
- catch (SmtpFailedRecipientsException ex)
- {
- foreach (SmtpFailedRecipientException t in ex.InnerExceptions)
- {
- var status = t.StatusCode;
- if (status == SmtpStatusCode.MailboxBusy ||
- status == SmtpStatusCode.MailboxUnavailable)
- {
- return View(status);
- }
- else
- {
- return View(status);
- }
- }
- }
- catch (SmtpException Se)
- {
- // handle exception here
- return View(Se);
- }
- catch (Exception ex)
- {
- return View(ex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement