E_A
E_A

Reputation: 131

Error Sending Email with SMTP: 'Failure Sending Mail' Despite Internet Connection

I'm trying to send an email with an attached file when a button is clicked, but I'm getting the error "Failure sending mail" even though I'm connected to the internet. I can't figure out what's wrong. Here's the code I'm using:

private void proto_Type_AI_(object sender, RoutedEventArgs e)
{
    try
    {
        // SMTP Client Details                    
        SmtpClient clientDetails = new SmtpClient();
        clientDetails.Port = Convert.ToInt32(port_number.Text.Trim());
        clientDetails.Host = smtp_server.Text.Trim();
        clientDetails.EnableSsl = ssl.IsChecked == true;
        clientDetails.DeliveryMethod = SmtpDeliveryMethod.Network;
        clientDetails.UseDefaultCredentials = false;
        clientDetails.Credentials = new NetworkCredential(sender_email.Text.Trim(), sender_password.Password.Trim());

        // Message Details
        MailMessage mailDetails = new MailMessage();
        mailDetails.From = new MailAddress(sender_email.Text.Trim());
        mailDetails.To.Add(recipient_email.Text.Trim());
        mailDetails.Subject = subject.Text.Trim();
        mailDetails.IsBodyHtml = html.IsChecked == true;
        mailDetails.Body = body.Text.Trim();

        clientDetails.Send(mailDetails);

        MessageBox.Show("Your message has been sent.");

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I'm using Google's SMTP server to send the email. Can anyone help me troubleshoot this issue?

Upvotes: 0

Views: 56

Answers (1)

timunix
timunix

Reputation: 629

using Microsoft.Office.Interop.Outlook; 

namespace YourNamespace
{
   public class YourClass
   { 
    //Create a mail object 
    Microsoft.Office.Interop.Outlook.Application ol = new  Microsoft.Office.Interop.Outlook.Application(); 
    //Create a mail item 
    Microsoft.Office.Interop.Outlook.MailItem mailitem = ol.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 
    //Your mail subject text 
    mailitem.Subject = "SMART G's Auto Email Test"; 
    //Mail Recipient 
    mailitem.To = "[email protected]"; 
    //Mail Sub Recipient 
    mailitem.CC = "[email protected]"; 
    //Your E-Mail Text 
    mailitem.Body = "Dear Sir or Madam,\n\n" + 
   "Attached please find my great manual on how to save the world. Thank you for reading. + "\n\n"
    + "Kind regards" + "\n\n" + "Smart G"; 
    //Your mail attachment
    mailitem.Attachments.Add(<<path>> + <<documentName>>); 
    //Do you want me to show you the mail?                           
    mailitem.Display(true); //yes 
    //If you want to check the mail before sending it then do not use this.     
    mailitem.Send(); //automatically sends the mail before you can check it! (autosend) 
    //Notifcation 
    MessageBox.Show("Mail sent!")
    }
}

Upvotes: 1

Related Questions