简体   繁体   English

C# 发送电子邮件错误。 如何用这种方式发送电子邮件?

[英]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;不要忘记使用 System.Net.Mail 添加; You need to add mail.From .您需要添加mail.From It's very important.这很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM