简体   繁体   中英

C# send e-mail error. How to send an e-mail with this way?

This code is not running. I want to send a mail, but ı can't.

I want to send an e-mail, when ı run the code. But it's not working. I realy neeed help.

public static void SendNotification(string filepath)
    {
        try
        {
            SmtpClient mailServer = new SmtpClient(ConfigurationManager.AppSettings["host"], int.Parse(ConfigurationManager.AppSettings["portnumber"]));
            mailServer.EnableSsl = true;
             System.Net.NetworkCredential(ConfigurationManager.AppSettings["sender_username"], ConfigurationManager.AppSettings["sender_password"]);

            string to = ConfigurationManager.AppSettings["RECEIVE"];
            string cc = ConfigurationManager.AppSettings["CC"];
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = "";
            msg.Body = "Test Mail.";
            mailServer.Send(msg);
        }
        catch (Exception ex)
        {
            //Log
        }
    }

you have missing things.

Here is

private static void SendMail(string subject, string content)
{
    try

    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("YOURMAİL");
        mail.To.Add("MAİLTO");
        mail.Subject = subject;
        mail.Body = content;
        SmtpServer.Port = 25;
        SmtpServer.Credentials = new System.Net.NetworkCredential("YOURMAİL", "YOURMAİLPASSWORD");
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    }
    catch (Exception ex)
    {

    }
}

This is simplest way to send mail. Don't forget to add using System.Net.Mail; You need to add mail.From . It's very important.

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