简体   繁体   中英

How to send Mail in asp.net from windows mail server

I have tried sending mail from gmail and other share hosting mail server and its works as expected But Now my requirement is sending mail from windows mail server.whenever i try to send mail it shows gives me error failure sending mail.

SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;

client.Host = "us2.webmail.mailhostbox.com";
string fromaddress = "info@csi.com";
client.Port = 25;
//client.Host = "mail.csisite.com";
// setup Smtp authentication
System.Net.NetworkCredential credentials =
    new System.Net.NetworkCredential("info@csisite.com", "password");
//client.UseDefaultCredentials = true;
client.Credentials = credentials;
client.EnableSsl = false;
try
{

//mail For Customer
MailMessage msgcustomer = new MailMessage();
msgcustomer.From = new MailAddress(fromaddress);
msgcustomer.To.Add(new MailAddress(EmailID));

msgcustomer.Subject = "Thank you for writing to us" ;
msgcustomer.IsBodyHtml = true;
msgcustomer.Body = "Test Mail";
client.Send(msgcustomer)

using System.Net.Mail

    MailMessage msgMail = new MailMessage();

    MailMessage myMessage = new MailMessage();
    myMessage.From = new MailAddress("sender's email","sender`s name and surname");
    myMessage.To.Add("recipient's email");
    myMessage.Subject = "Subject";
    myMessage.IsBodyHtml = true;

    myMessage.Body = "Message Body";


    SmtpClient mySmtpClient = new SmtpClient();
    System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
    mySmtpClient.Host = "your smtp host address";
    mySmtpClient.UseDefaultCredentials = false;
    mySmtpClient.Credentials = myCredential;
    mySmtpClient.ServicePoint.MaxIdleTime = 1;

    mySmtpClient.Send(myMessage);
    myMessage.Dispose();

updated user is getting the following error

{"A socket operation was attempted to an unreachable network 208.91.199.230:25"} and Error Code is 10051

The Error 10051 is a socket error that appears when the target network or host server can not be reached. The error could have occurred due to an erroneous configuration of the router, Internet disconnection, or a blocking action by a firewall. This error has occurred mostly in attempts to generate Internet Control Message Protocol (ICMP) and Simple Mail Transfer Protocol (SMTP) connections with wrong configurations. This error also indicates that the Windows software employed is configured to communicate to a router, since the 10065 error appears instead if Windows is not set to link to a router.

you should make ensure that the network path is correct, that the computer is not running two software firewalls and that the targeted computer is not turned off.

Send Mail:

var smtp = new System.Net.Mail.SmtpClient();
{
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
    smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);

Use This Code for sending mail this is tested code.

 public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();

        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));

        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;

        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;

        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        mSmtpClient.Credentials = new System.Net.NetworkCredential
             ("YourEmail@gmail.com", "Password");
        //Or your Smtp Email ID and Password
        mSmtpClient.EnableSsl = true;

        mSmtpClient.Send(mMailMessage);
    }

Note: I have been trying to send email through my ISP mail server with no success - following all the suggestions that I could find on these forums. I just discovered that it would work fine if I did not attempt to alter the default credentials setting. If I included either: client.UseDefaultCredentials = true; or client.UseDefaultCredentials = false; in my code, then I would get a login failure message from the server. By not including either, it worked fine.

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