通过C#发送电子邮件 [英] Sending e-mail throught c#

查看:173
本文介绍了通过C#发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现此代码是用于通过SMPT发送邮件的C#,但是我应该在哪里找到该代码消息?

使用ASP.NET和C#发送邮件/联系表 [

Hi,

I found this code is C# for sending mail through SMPT, but where should I found the code message?

Send Mail / Contact Form using ASP.NET and C#[^]

And this is the HTML code:

<%this is the client side code for the design and display%>
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
    <p>
        Please Fill the Following to Send Mail.</p>
    <p>
        Your name:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"

            ControlToValidate="YourName" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
        Your email address:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"

            ControlToValidate="YourEmail" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
        <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"

            SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"

            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"

            ValidationGroup="save" /><br />
        Subject:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"

            ControlToValidate="YourSubject" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
        Your Question:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"

            ControlToValidate="Comments" ValidationGroup="save" /><br />
        <asp:TextBox ID="Comments" runat="server" 

                TextMode="MultiLine" Rows="10" Width="400px" />
    </p>
    <p>
        <asp:Button ID="btnSubmit" runat="server" Text="Send" 

                    OnClick="Button1_Click" ValidationGroup="save" />
    </p>
</asp:Panel>
<p>
    <asp:Label ID="DisplayMessage" runat="server" Visible="false" />
</p>  



这是服务器端代码:



This is the server side code:

protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString(); 
    //Password of your gmail address
    const string fromPassword = "Password";
     // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //here on button click what will done 
        SendMail();
        DisplayMessage.Text = "Your Comments after sending the mail";
        DisplayMessage.Visible = true;
        YourSubject.Text = "";
        YourEmail.Text = "";
        YourName.Text = "";
        Comments.Text = "";
    }
    catch (Exception) { }
}

推荐答案

对于发送邮件来说效果很好:
我认为您想通过邮件发送邮件
比您可以使用的以下内容.
您的smtp代码照常编写.
for Send Mail it good :
i think you want to send mail by mail message
than you can used following.
your smtp code write as usual.
var smtp = new System.Net.Mail.SmtpClient();
  {
      smtp.Host = "smtp.gmail.com";
      smtp.Port = 587;
      smtp.EnableSsl = true;
      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
      smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
      smtp.Timeout = 20000;
  }
than you write 
     MailMessage mailObj = new MailMessage();
 mailObj.Priority = MailPriority.High;

                MailAddress mailfrom = new MailAddress(addFrom, UserName);
                mailObj.From = mailfrom;
  MailAddress mailadTo2 = new MailAddress(txtEmailTo);

                    mailObj.To.Add(mailadTo2);
  mailObj.Subject = txtSubject;
                mailObj.IsBodyHtml = true;
  mailObj.Body = Body;
 smtp.Send(mailObj);
it send mail i hope you want it if not than send comments.]


我建​​议您通读一些这些主题中的文章.我想您会找到想要的东西.

http://www.codeproject.com/search.aspx?q=C%23+SMTP+&sbo=kw&usfc=false&x=0&y=0 [
I would recommend reading through some of these articles on the subject. I think you will get a grasp on what you are looking for.

http://www.codeproject.com/search.aspx?q=C%23+SMTP+&sbo=kw&usfc=false&x=0&y=0[^]


这篇关于通过C#发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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