开发者

ASP.NET: Send Mail

Im trying to send a mail from a asp.net script, but im getting this error:

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 4.1.8 : Sender address rejected开发者_运维问答: Domain not found at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at Clematis_ADO.sites.Kvittering.Page_Load(Object sender, EventArgs e)

As i understand this, it can't use the smtp server that i specified, but im using the smtp server giving by my webhost. So what could the error be?

This is tested on the webhost, not localhost.

And the code im using for this:

 try
         {
             StringWriter sw = new StringWriter();
             HtmlTextWriter w = new HtmlTextWriter(sw);
             OrderConfirmationContainer.RenderControl(w);
             string s = sw.GetStringBuilder().ToString();

             MailMessage mail = new MailMessage();

             string emailSubject = "Testing";

             mail.To.Add(new MailAddress(order.getFname() + " " + order.getLname() + "<"+order.getEmail()+">"));
             mail.Sender = new MailAddress("****** <***@***.com>");
             mail.From = new MailAddress("***@***.com");
             mail.Subject = emailSubject;
             mail.IsBodyHtml = true;
             mail.Body = "<h3>" + emailSubject + "</h3>" + s;

             SmtpClient smtp = new SmtpClient();
             smtp.Host = "mailoutb1.surf-town.net";
             smtp.Send(mail);

         }
         catch (Exception exception)
         {
             if(exception != null)
                MailLabel.Text = "Mail fejlen er: " + exception.ToString();
         }


The error you are getting seems to be because the SMTP server is actually rejecting the email because the domain name you are trying to send to or from is either wrong or is blacklisted.

Are you using a fake email address to test with or a real one (please tell me you aren't really trying to send an email to or from ****@****.com)? If it's real, you'll likely have to call your ISP and see what blacklist they are using and see if the domain you are trying to send to is blacklisted.


I know some webhosts lock down their smtp servers to only allow mail to be sent when the "sender" is a valid mailbox in the domain. Is your mail.sender value a valid mailbox?


If you want to supply both an address and a display name, it is easier to use the constructor that takes two parameters instead of conjoining the two yourself.

mail.To.Add(new MailAddress( order.getEmail(), order.getFname() + " " + order.getLname()));

I know that this doesn't solve your problem, but I thought I would mention it.


A bit of necessary background: SMTP servers "relay" messages by design i.e. you send a message to an SMTP server and it sees if its for a "local" recipient and if not it forwards it to another server for delivery (determined by MX records etc).

Unfortunately this makes (well made) things to easy for spammers so now SMTP servers tend to be locked down so that one of the following conditions has to be met:

  1. The message is for a known/local recipient
  2. The sending computer is on/within the same network (for the sake of discussion) as the SMTP server or
  3. The sender is authenticated (username/password or similar) in some way.

Some are even more picky and require that they know the sending ("from") domain as well.

The reason for all the above is that the error you are seeing is basically the same error I see from my provider (of servers and hosting) if I attempt to send mail to a third party without authenticating first (either from one of our dedicated servers or from an email client).

If you're using generic hosted web space and getting this error then you need to check with the host (help pages, knowledgebase, whatever) to see how they expect you to be able to send email from your applications.

There does appear to be scope within SmtpClient for setting credentials, however I haven't found this necessary so can't be much help with the details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜