从asp.net表单发送电子邮件到电子邮件ID? [英] Sending email from asp.net form to an Email id?

查看:74
本文介绍了从asp.net表单发送电子邮件到电子邮件ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中有一个表单,其中点击按钮数据被发送到电子邮件ID。但是电子邮件没有发送。请查看代码并提出建议..

I have a form in asp.net in which on click of a button data is sent to a email id. But the email is not sent. Please have a look at the code and suggest..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("receiver@gmail.com");
            //Configure the address we are sending the mail from
            MailAddress address = new MailAddress("sender@gmail.com");
            msg.From = address;
            //Append their name in the beginning of the subject
            msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
            msg.Body = txtMessage.Text;

            //Configure an SmtpClient to send the mail.
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true; //only enable this if your provider requires it
            //Setup credentials to login to our sender email address ("UserName", "Password")
            NetworkCredential credentials = new NetworkCredential("sender@gmail.com", "password");
            client.Credentials = credentials;

            //Send the msg
            client.Send(msg);

            //Display some feedback to the user to let them know it was sent
            lblResult.Text = "Your message was sent!";

            //Clear the form
            txtName.Text = "";
            txtMessage.Text = "";
        }
        catch
        {
            //If the message failed at some point, let the user know
            lblResult.Text = "Your message failed to send, please try again.";
        }

    }
}

推荐答案

using (MailMessage mm = new MailMessage(fromEmail, toEmail))
{
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(fromEmail, password);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
}





试试这个



Try this


在outlook中配置发件人邮件然后在发送邮件时你可以从outlook note设置端口发送no等,然后在你的代码中使用它们。



试一试
Configure sender mail in outlook then send mail when u can send from outlook note settings port no etc then use them in your code.

Give it a try


因为你正在使用POP3,所以你还需要在你的代码中提到它。
Because you are using POP3, so you also need to mention that in your code as well.


这篇关于从asp.net表单发送电子邮件到电子邮件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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