0

I am trying to send an email with Asp.Net but the email will not always be sent using the same WIFI credentials.

string email = "noreply@sender.com";
string subject = "Subject";
string MailContent = "This is the content";

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add(email);
mail.From = new MailAddress("noreplay@reciever.com");
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = MailContent;
SmtpServer.Host = "smtpserver";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
    SmtpServer.Send(mail);
}
catch (Exception ex)
{
    MessageBox.Show("Exception Message: " + ex.Message);
    if (ex.InnerException != null)
        MessageBox.Show("Exception Inner:   " + ex.InnerException);
}

I tried using this code, which was found on a different question but still can't seem to get an error. Any alternative can work, I have a pdf which would like to be sent on an email or even one drive if possible. Thanks

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Does the sender email account exists at the mail server? – derloopkat Sep 03 '20 at 09:09
  • What errors are thrown? – phuzi Sep 03 '20 at 09:46
  • @derloopkat I don't know what you mean by that. – Alvin Cassar Sep 03 '20 at 10:02
  • @phuzi the error thrown is, The remote name could not be resolved: ''smptHost''. – Alvin Cassar Sep 03 '20 at 10:03
  • @AlvinCassar, for you to send emails you need to have an email account in a mail server and of course the mail server should exist. – derloopkat Sep 03 '20 at 10:12
  • @derloopkat so i can send one to myself to see if that works right? – Alvin Cassar Sep 03 '20 at 10:35
  • But I still get the same error, I changed the SmtpServer.Host to "smtp.smtpgmail.com" and i get the error https://imgur.com/RoUzO68 – Alvin Cassar Sep 03 '20 at 10:40
  • @AlvinCassar probably because gmail uses SSL. You need proper settings and it's a bit more complicated with gmail. Check out [this answer](https://stackoverflow.com/a/707892/2516718) and also comments below this answer as you might need to allow access for this account from gmail page. – derloopkat Sep 03 '20 at 11:17
  • Does this answer your question? [Sending email through Gmail SMTP server with C#](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – DCCoder Sep 03 '20 at 23:23

0 Answers0