send email asp.net c#

后端 未结 8 580
别跟我提以往
别跟我提以往 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:50

    Send Email with Attachment using asp.net C#

      public void Send(string from, string to, string Message, string subject, string host, int port, string password)
        {
            MailMessage email = new MailMessage();
            email.From = new MailAddress(from);
            email.Subject = subject;
            email.Body = Message;
            SmtpClient smtp = new SmtpClient(host, port);
            smtp.UseDefaultCredentials = false;
            NetworkCredential nc = new NetworkCredential(txtFrom.Text.Trim(), password);
            smtp.Credentials = nc;
            smtp.EnableSsl = true;
            email.IsBodyHtml = true;
    
            email.To.Add(to);
    
            string fileName = "";
            if (FileUpload1.PostedFile != null)
            {
                HttpPostedFile attchment = FileUpload1.PostedFile;
                int FileLength = attchment.ContentLength;
                if (FileLength > 0)
                {
                    fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    FileUpload1.PostedFile.SaveAs(Path.Combine(Server.MapPath("~/EmailAttachment"), fileName));
                    Attachment attachment = new Attachment(Path.Combine(Server.MapPath("~/EmailAttachment"), fileName));
                    email.Attachments.Add(attachment);
                }               
            }
            smtp.Send(email);
    
        }
    

    for complete tutorial step by step (with Video) visit http://dotnetawesome.blogspot.in/2013/09/send-email-with-attachment-using-cnet.html

提交回复
热议问题