开发者

Send mail using SMTP server

开发者 https://www.devze.com 2023-04-03 06:32 出处:网络
Hell Guys! I have used SMTP server to send mail as per below public bool SendMail(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, string attachment, boo

Hell Guys!

I have used SMTP server to send mail as per below

 public bool SendMail(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, string attachment, bool isBodyHtml)
{
    bool SendStatus = false;
    System.Net.Mail.MailMessage mailMesg = new System.Net.Mail.MailMessage(mailFrom, mailTo);

    if (mailCC != string.Empty)
        mailMesg.CC.Add(mailCC);

    if (mailBCC != string.Empty)
        mailMesg.Bcc.Add(mailBCC);

    if (!string.IsNullOrEmpty(attachment))
    {
        System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(attachment);
        mailMesg.Attachments.Add(attach);
    }
    mailMesg.Subject = subject;
    mailMesg.Body = body;
    mailMesg.IsBodyHtml = isBodyHtml;
    mailMesg.ReplyTo = new System.Net.Mail.MailAddress(mailFrom);

    System.Net.Mail.SmtpClient objSMTP = new System.Net.Mail.SmtpClient();


    string Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();
    string UserName = System.Configuration.ConfigurationManager.AppSettings["MailUserId"].ToString();
    string password = System.Configuration.ConfigurationManager.AppSettings["MailPassword"].ToString();


    objSMTP.Host = Host;
    objSMTP.Port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"].ToString());
    objSMTP.Credentials = new System.Net.NetworkCredential(UserName, password);


    objSMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

    try
    {
        objSMTP.Send(mailMesg);
        SendStatus = true;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        mailMesg.Dis开发者_运维技巧pose();
        mailMesg = null;
    }


    return SendStatus;

}

I would like to know that Is it possible to send mail without username & password ? If possible then can anyone please suggest me how to do that?


Sure, if your smtp server doesn't require cridentials you shouldn't specify em. Otherwise you should.


I think you need to white list the ip that you will send e-mail from which will be your server and this option is done on the mail account that you are going to send e-mails from.

0

精彩评论

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