如何从网页发送邮件 [英] how to send a mail from web page

查看:95
本文介绍了如何从网页发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class feed_back : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailmessage = new MailMessage();
            mailmessage.To.Add(TextBox1.Text);
            mailmessage.From = new MailAddress(TextBox2.Text);
            mailmessage.Subject = TextBox3.Text;
            mailmessage.Body = TextBox4.Text;
            SmtpClient smtpclient = new SmtpClient("");
           
            smtpclient.Credentials = new System.Net.NetworkCredential("", "smtpclient");
            smtpclient.Send(mailmessage);
            Label5.Text = "Email sent";
             
        }
        catch (Exception ex)
        {
            Label5.Text = "Could not send email-error:" + ex.Message;

        }
    }
}


我正在使用此代码.显示错误.

"Could not send email-error:The SMTP host was not specified."

我已经在IIS中完成了所有的smtp配置设置.

解决此错误的方法是什么.


I am using this code. Its displaying error.

"Could not send email-error:The SMTP host was not specified."

I had done all the smtp configure settings in IIS.

What is the solution for this error.

推荐答案

您需要提供SMTP服务器名称,希望这段代码可以帮助您理解

You need to provide the SMTP server name, I hope this piece of code might help you in understanding

using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address@mfc.ae");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}


尝试一下,

Try this,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.Windows.Forms;

namespace DataSimulator
{
    class EmailFunctions
    {

        //ayarlar
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("frommailaddress@gmail.com", "frommailpassword");

        public void SendMail(String sendTo, String sendFrom, String subject, String body)
        {
            try
            {
                //smtp client configurations
                client.Host = "smtp.gmail.com";
                client.Port = 587; //gmail smtp port number "587"
                client.UseDefaultCredentials = false;
                client.Credentials = smtpCreds;
                client.EnableSsl = true;

                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                //mail mesajı ayarları
                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add(to);

                //gönderme işlemi
                client.Send(msg);
                MessageBox.Show("successfull", "success");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"unsuccessfull!");
            }
        }
        
    }
}


使用此代码为您的邮件内容和类创建html以发送邮件

Use this code create html for your mail content and class to send mail

namespace MailerService
{
    public class MailerController
    {
          public static void SendUserConfirmationMail(User _user)
        {
            Mailer mailers = new Mailer();
            mailers.RecipientsEmailId = "abc@abc.com";
            mailers.RecipientsName = _"Test";
            mailers.Subject = "Testing Mail";
            mailers.MessageSendingTime = DateTime.Now;
            mailers.UserName = "Abc";
            MailHelper mailHelper = new MailHelper();
            mailHelper.SendMail(mailers, "Test.htm");
        }
    }
 }





namespace MailerService
{
    public class MailHelper
    {
        #region Public variable
        public string MailTo
        { get; set; }
        public string MailFrom
        { get; set; }
        public string MailSubject
        { get; set; }
        public string MailBody
        { get; set; }
        public string MailKey
        { get; set; }
        #endregion

        public MailHelper()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void SendMail(Mailer mailers, string filePath)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(System.IO.File.ReadAllText(Settings.GetApplicationRootPath() + "HtmlMails//" + filePath));
            sb = sb.Replace("Identifier1", mailers.Identifier1);
            sb = sb.Replace("Identifier2", mailers.Identifier2);
            sb = sb.Replace("Identifier3", mailers.Identifier3);
            sb = sb.Replace("Identifier4", mailers.Identifier4);

            MailTo = mailers.RecipientsEmailId;
            MailFrom = WebConfigurationManager.AppSettings["SmtpServerUserName"];
            MailSubject = mailers.Subject;
            MailBody = sb.ToString();
            SendMail();
        }



        #region To send mail
        public void SendMail()
        {
            MailHelper objMail = new MailHelper();
            objMail.MailBody = this.MailBody;
            objMail.MailFrom = this.MailFrom;
            objMail.MailSubject = this.MailSubject;
            objMail.MailTo = this.MailTo;
            Thread th = new Thread(delegate() { ThreadSend(objMail); });
            th.Start();
        }
        public void ThreadSend(MailHelper obj)
        {
            SmtpClient smtp = new SmtpClient();
            MailMessage eMail = new MailMessage();
            eMail.IsBodyHtml = true;
            eMail.From = new MailAddress(obj.MailFrom);
            eMail.To.Add(obj.MailTo);
            smtp.Host = WebConfigurationManager.AppSettings["SmtpServer"];    //Settings.SmtpServer;
            smtp.EnableSsl = false;   // Settings.SmtpServerSSL;
            eMail.Body = obj.MailBody;
            eMail.Subject = obj.MailSubject;
            smtp.Port = WebConfigurationManager.AppSettings["SmtpServerPort"].ToInteger();   // Settings.SmtpServerPort;
            smtp.Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["SmtpServerUserName"], WebConfigurationManager.AppSettings["SmtpServerPassword"]);
            smtp.Send(eMail);
        }

        void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}


这篇关于如何从网页发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆