简体   繁体   中英

Transaction failed. The server response was: SMTP Host

I've been trying to make a simple eMail sending APP and I'm blocked with this error, I can't seem to find anyone with this problem (Kinda clueless of what to search though)

So I'm having this error when I try to send my eMail, it just returns me this error with the name of my SMTP Host,

Transaction failed. The server response was: smtp.host.com

If I look at the trace it crashes at

at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)

Here's the code sample, nothing fancy.

MailMessage mail = new MailMessage();
mail.From = new MailAddress(CurrentUser.email);
mail.Subject = txtSubject.Text;
mail.Body = txtMailBody.Text;

try
{
    foreach (ListItem user in lbSelectedUsr.Items)
    { 
        mail.To.Add(new MailAddress(user.email));
    }

    SmtpClient mailClient = new SmtpClient("smtp.host.com");
    mailClient.Send(mail);

    lblResultOK.Visible = true;
}
catch(Exception ex)
{
    lblResultOK.Visible = true;
}

Note that the SMTP is fake for the sake of the sample.

I tried with invalid SMTP and it gave me different error so the SMTP seems to be OK, I also tried to put credentials, same error. Any hint on what's happening?

I your SMTP config set properly in Web.Config file? I would say try to send a sample email via host="smtp.gmail.com"

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="emailAddress">
    <network host="smtp.gmail.com" port="587" userName="emailAddress" password="password" defaultCredentials="false"/>
  </smtp>
</mailSettings>

Below code must be placed in web.config. The attention point is defaultCredentials="false"

<system.net>
<mailSettings>
  <smtp from="aaa@yourdomain.com">
    <network host="mail.yourdomain.com" userName="aaa@yourdomain.com" password="your password" port="587" defaultCredentials="false"/>
  </smtp>
</mailSettings>

Try send by google smtp, and if every one is ok then change it to your smtp server info.If you do it and If your problem is still in place, then you must check your smtp server.

The server does not accept relay from the machine that runs your app. Configure the server appropriately, or run the code on the machine that is already configured for relay (usually all computers within the domain; I suppose that your computer belongs to a different domain than SMTP server).

MailMessage mail = new MailMessage();
mail.From = new MailAddress(CurrentUser.email);
mail.Subject = txtSubject.Text;
mail.Body = txtMailBody.Text;

try
{
    foreach (ListItem user in lbSelectedUsr.Items)
    { 
        mail.To.Add(new MailAddress(user.email));
    }

    SmtpClient mailClient = new SmtpClient("smtp.host.com");
    mailClient.EnableSsl = true
    mailClient.Send(mail);

    lblResultOK.Visible = true;
}
catch(Exception ex)
{
    lblResultKO.Visible = true;
}

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