简体   繁体   中英

Send email from ASMX web service

Recently somebody answered me on this site, that this method can send email from .net application:

public static void SendEmail(bool isHTML, string toEmail, string fromEmail, string subject, string message)
{
    var sm = new SmtpClient("smtp.mail.ru");
    sm.Credentials = new NetworkCredential("MyLogin", "MyPass");
    var m = new MailMessage(fromEmail, toEmail) { Subject = subject, Body = message };
    if (isHTML)
    {
        m.IsBodyHtml = true;
    }
    sm.Send(m); // SmtpException
}

It is true. But now I want to use this method from Asp.Net WebService, but I have SmtpException at last string. Why? And do I send email from web service.

So the problem is not with your code, rather the transaction with the SMTP server is failing for some reason. If you have access to the SMTP server, check its logs. Otherwise you might have to use a sniffer like WireShark to figure it out.

To verify this, you can try using a different mail server, assuming you have proper access to that server it should send the mail properly.

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