简体   繁体   中英

C# Send email using SMTP

Exception:

Failure sending mail.

Inner Exception:

Unable to connect to the remote server.

MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("abc@gmail.com");
mail.To.Add("xyz@yahoo.co.in");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("abc@gmail.com", "xxx");
smtp.Credentials = netCre;

try
{
     smtp.Send(mail);
 }
 catch (Exception ex)
 {
 }

I ping smtp.gmail.com it works fine.

MailMessage mm = new MailMessage();
SmtpClient smtp = new SmtpClient(); 

mm.From = new MailAddress("From", "DisplayName", System.Text.Encoding.UTF8);
mm.To.Add(new MailAddress("To"));
mm.Subject = "Subject";
mm.Body = "Body";

mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
if (ccAdd != "")
{
    mm.CC.Add(ccAdd);
}
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "xyz@gmail.com";//gmail user name
NetworkCred.Password = "Password";// password
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //Gmail port for e-mail 465 or 587
smtp.Send(mm);

It will work fine

Hint for windows 7 / visual studio 2019 developers: The code below doesn't work with .NET 4.8.03761 but with .NET 4.6.1 < 08.11.2022>
It works on windows 10 / visual studio 2022 and .NET 4.8.04084 -> I reimported the file from windows 10 to windows 7 and then choosed for the reimport environment .NET 4.6.1 !!!

private void btnSendEmail_Click(object sender, EventArgs e)
{
    MailMessage Mail = new MailMessage();
    
    Mail.From = new MailAddress("anyone.name@gmx.de");
    Mail.To.Add("anyone_else.name@gmx.de");
    Mail.Subject = "This is a test email from a C# program.";
    Mail.Body = "win7_ABCDEFG----hgfgfhgfhgjjhköhökhgjhfhgfhgk\nhgjgjhgjhgj";
    // Mail.Bcc.Add("otto.waalkes@gmx.de");       // choose here additionaly anyone
    // Mail.CC.Add("....");

    SmtpClient smtpClient = new SmtpClient("mail.gmx.net");   //  also: mail.gmx.de

    smtpClient.Port = 587;                                      // 587;    also:  25
    smtpClient.Credentials = new NetworkCredential("anyone.name@gmx.de", "password/2022");
    smtpClient.EnableSsl = true;     // recomended for SSL encryption with 465 -> doesn't work
    
    // https://www.wolfgang-frank.eu/mailserver.php    a long list of german mailserver

    try
    {
        smtpClient.Send(Mail);                
    }
    catch (Exception ex)
    {
        Console.Beep();
        MessageBox.Show(ex.ToString());
    }
}

Correction: Hint for windows 7 / visual studio 2019 developers: The code below doesn't work with .NET 4.7.2 (Visual Studio 2019 /poject explorer / project / rigth mouse / properties / target framework) but with.Net 4.6.1

Additional, I had to set in the E-Mail browser of the sender - here anyone.name@gmx.de / left down - "Einstellungen"(settings) / "POP3/IMAP Abruf" / "das Häckchen" (the check mark) X "GMX Mail über POP3 & IMAP erlauben" <18.12.2022>

Email Script By ASP.Net

//mail script
           string name = TextBox1.Text;
           string email = TextBox3.Text;
           string feedback = TextBox5.Text;
           System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
           message.To.Add("info@globalestcon.com");
           message.Subject = "Email from website";
           message.From = new System.Net.Mail.MailAddress(email);
           message.Body = feedback;
           System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("relay-hosting.secureserver.net");
           smtp.Send(message);
           TextBox1.Text = "";
           TextBox2.Text = "";
           Response.Write("<script>alert('Form Submitted');</script>");

I hope it will help you. its working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM