send email asp.net c#

后端 未结 8 621
别跟我提以往
别跟我提以往 2020-12-08 22:59

Is it possible to send email from my computer(localhost) using asp.net project in C#? Finally I am going to upload my project into webserver but I want to test it before upl

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 23:48

    Create class name SMTP.cs then
    
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Net;
    
    
    
    /// 
    /// Summary description for SMTP
    /// 
    public class SMTP
    {
        private SmtpClient smtp;
    
        private static string _smtpIp;
        public static string smtpIp
        {
            get
            {
                if (string.IsNullOrEmpty(_smtpIp))
                    _smtpIp = System.Configuration.ConfigurationManager.AppSettings["smtpIp"];
    
                return _smtpIp;
    
            }
        }
    
    
        public SMTP()
        {
            smtp = new SmtpClient(smtpIp);
         }
    
        public string Send(string From, string Alias, string To, string Subject, string Body, string Image)
        {
            try
            {
                MailMessage m = new MailMessage("\"" + Alias + "\" <" + From + ">", To);
                m.Subject = Subject;
                m.Priority = MailPriority.Normal;
    
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(Body, System.Text.Encoding.UTF8, MediaTypeNames.Text.Html);
    
                if (!string.IsNullOrEmpty(Image))
                {
                    string path = HttpContext.Current.Server.MapPath(Image);
                    LinkedResource logo = new LinkedResource(path, MediaTypeNames.Image.Gif);
                    logo.ContentId = "Logo";
                    av1.LinkedResources.Add(logo);
                }
    
                m.AlternateViews.Add(av1);
                m.IsBodyHtml = true;
    
                smtp.Send(m);
            }
            catch (Exception e)
            {
                return e.Message;
            }
    
            return "sucsess";
        }
    }
    
    then 
    
    on aspx page
    
    protected void lblSubmit_Click(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.ContentType = "text/plain";
            //Guid guid = Guid.NewGuid();
            string EmailMessage = "" +
                                          "" +
                                              "" +
                                          "" +
                                           "" +
                                           "" +
                                                  "

    " + "

    Name: " + nameID.Value + ",

    " + "

    Email: " + EmailID.Value + ",

    " + "

    Comments: " + commentsID.Text + "

    " + // "Welcome to the Test local updates service!
    Before we can begin sending you updates, we need you to verify your address by clicking on the link below.
    " + //"

    " + //"We look forward to keeping you informed of the latest and greatest events happening in your area.
    " + //"If you have any questions, bug reports, ideas, or just want to talk, please contact us at

    " + //"Enjoy!
    " + commentsID.Text + "
    " + //"Test
    www.Test.com

    " + "" + ""; lblThank.Text = "Thank you for contact us."; // string Body = commentsID.Text; SMTP smtp = new SMTP(); string FromEmail = System.Configuration.ConfigurationManager.AppSettings["FromEmail"]; string mailReturn = smtp.Send(EmailID.Value, "", FromEmail, "Contact Us Email", EmailMessage, string.Empty); //HttpContext.Current.Response.Write("true"); nameID.Value = ""; EmailID.Value = ""; commentsID.Text = ""; }

提交回复
热议问题