sending email to gmail from asp.net c# webpage

Discussion in 'Email' started by MarkBrend, May 22, 2013.

  1. Hi,
    Something strange is happening.
    When I send an email to myself Postmaster@mydomain.com it work fine. So the logging in bit is working.
    However; when I send an email to my gmail account I get Mailbox unavailable. The server response was: <myemail@gmail.com> No such user here
    I know the gmail email address is valid as I used it everyday
    I am obviously not doing something or have to do something to keep gmail happy
    public static void Send(string strServer, string strTo, string strFrom, string strReplyTo, string strSubject, string strEmailMess)
    {

    MailMessage mail = new MailMessage();
    mail.To.Add(strTo);
    mail.From = new MailAddress(strFrom);
    mail.Subject = strSubject;
    mail.Body = strEmailMess;
    mail.IsBodyHtml = false;

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "mail.MyDomain.com";
    smtp.Credentials = new System.Net.NetworkCredential("postmaster@MyDomain.com", "passwordnot");
    smtp.EnableSsl = false;
    smtp.Send(mail);


    }
     
  2. Ray

    Ray

    The symptoms you are having means that SMTP authentication is not being passed correctly. If you send email to yourself, SMTP authentication is not triggered and that is why you think you are logging into the SMTP service correctly.
    Check your code again and make sure you are passing the SMTP authentication correctly.
    I am assuming you are not using "Postmaster@mydomain.com" as SMTP authentication; correct?
     
  3. I have the exact same issue right now.
    What is the problem. The code is the same that's been running for years.
     
  4. Ray

    Ray

    You may have reset the password to the POP account you are using for SMTP authentication. If that is the case, you should also update the SMTP Authentication credentials on the code.
     
  5. Verified it's the correct credentials, by logging in to webmail with it.
    Anything else?
     
  6. This is how I send gmail

    private bool SendEmail(string myIPAddress)
    {
    bool emailsent = true;
    if (myemailaddress != "" && mypassword != "")
    {
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add(myemailaddress);
    mail.CC.Add("whoever@yahoo.com");
    mail.From = new MailAddress("do.not.reply@whatever.com", "do.not.reply@whatever.com", System.Text.Encoding.UTF8);
    mail.Subject = "Type Subject Here";
    mail.SubjectEncoding = System.Text.Encoding.UTF8;
    mail.Body = "Your IP address has changed to " + myIPAddress;
    mail.BodyEncoding = System.Text.Encoding.UTF8;
    mail.IsBodyHtml = true;
    mail.Priority = MailPriority.High;
    SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("yourGmailEmail@gmail.com", "thepasswordforthataccount");
    client.Port = 587; // Gmail works on this port
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true; //Gmail works on Server Secured Layer
    try
    {
    client.Send(mail);
    }
    catch (Exception ex)
    {
    emailsent = false;
    }
    }
    else
    {
    emailsent = false;
    }
    return emailsent;
    }
     
  7. Thanks William, but I already have gmail smtp as a fallback.

    It's the Winhost smtp that doesn't work.
     
    Last edited by a moderator: Oct 14, 2015
  8. Tabitha

    Tabitha WinHost HBIC

    What is your domain name?
     
  9. Sending email with Winhost

    This is how I send email using Winhost and MVC 4 .net

    public void Send()
    {

    MailMessage mail = new MailMessage();
    mail.To.Add("toEmailaddress@whatever.com");
    mail.CC.Add("toEmailaddress@whatever.com");
    mail.From = new MailAddress("FromEmailaddress@yourDomain.com", "Your Display Name", Encoding.UTF8);
    mail.Subject = "Subject Text";
    mail.SubjectEncoding = Encoding.UTF8;
    mail.Body = "Body Text";
    mail.BodyEncoding = Encoding.UTF8;
    mail.IsBodyHtml = true;
    mail.Priority = MailPriority.High;
    SmtpClient smtp = new SmtpClient("mail.yourDomain.com");
    NetworkCredential Credentials = new NetworkCredential("postmaster@yourDomain.com", "password");
    smtp.Credentials = Credentials;

    try
    {
    smtp.Send(mail);
    }
    catch (Exception ex)
    {
    le.LogException(ex, "Error sending email);
    }

    }

    My domain is ambiesoft.com

    Thank you
     
    Last edited by a moderator: Oct 14, 2015
  10. Ray

    Ray

    This maybe a stupid question but are you using 'mail.yourDomain.com' in your code. Believe or not some people make this mistake.
     
  11. I have sent you a pm with the details.
     
  12. This one worked for me

    mail.From = new MailAddress("your_email_address@gmail.com");
    mail.To.Add("to_address");
    mail.Subject = "Test Mail";
    mail.Body = "This is for testing SMTP mail from GMAIL";
    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
    SmtpServer.EnableSsl = true;
    src: http://csharp.net-informations.com/communications/csharp-smtp-mail.htm c# smtp email
     
  13. Here is a Email class that can be used with ASP.NET MVC4 leveraging dependency injection. A full running sample application & unit tests that use this class can be found here in my github spacehttps://github.com/fredo007/i6technology/tree/master/InsuranceSales.

    I've also put together an article explaining methodology & use here http://prestoasp.net/how-to-send-email-using-gmail-smtp-in-an-asp-net-mvc-application/
    public class GmailEmailService : IEmailService
    {
    private readonly SmtpConfiguration _config;

    private const string GmailUserNameKey = "GmailUserName";
    private const string GmailPasswordKey = "GmailPassword";
    private const string GmailHostKey = "GmailHost";
    private const string GmailPortKey = "GmailPort";
    private const string GmailSslKey = "GmailSsl";

    public GmailEmailService()
    {
    _config = new SmtpConfiguration();
    var gmailUserName = ConfigurationManager.AppSettings[GmailUserNameKey];
    var gmailPassword = ConfigurationManager.AppSettings[GmailPasswordKey];
    var gmailHost = ConfigurationManager.AppSettings[GmailHostKey];
    var gmailPort = Int32.Parse(ConfigurationManager.AppSettings[GmailPortKey]);
    var gmailSsl = Boolean.Parse(ConfigurationManager.AppSettings[GmailSslKey]);
    _config.Username = gmailUserName;
    _config.Password = gmailPassword;
    _config.Host = gmailHost;
    _config.Port = gmailPort;
    _config.Ssl = gmailSsl;
    }

    public bool SendEmailMessage(EmailMessage message)
    {
    var success = false;
    try
    {
    var smtp = new SmtpClient
    {
    Host = _config.Host,
    Port = _config.Port,
    EnableSsl = _config.Ssl,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(_config.Username, _config.Password)
    };

    using (var smtpMessage = new MailMessage(_config.Username, message.ToEmail))
    {
    smtpMessage.Subject = message.Subject;
    smtpMessage.Body = message.Body;
    smtpMessage.IsBodyHtml = message.IsHtml;
    smtp.Send(smtpMessage);
    }

    success = true;
    }
    catch (Exception ex)
    {
    //todo: add logging integration
    //throw;
    }

    return success;
    }
    }
     
    Michael likes this.

Share This Page