从asp.net发送电子邮件到gmail [英] sending email to gmail from asp.net

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

问题描述

//您可以指定服务器的主机名或ipaddress

// IIS中的默认值为localhost \

// smtpClient.Host =smtp .google.com;



目前我正在使用:smtpClient.Host =localhost;



i获取邮件到gmail id,但它也会收到错误,因为假电子邮件ID报告更多,所以如果我不想得到我必须做的而不是本地主机





从哪里获得主机名.. ??

哪个ip地址我必须给gmail或主机提供商.. ??

以及为什么我收到该错误此消息可能未发送者:test@gmail.com了解更多报告网络钓鱼.. ??



检查以下代码:



 使用系统; 
使用 System.Configuration;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.HtmlControls;
使用 System.Net.Mail;

public partial class _Default:System.Web.UI.Page
{
#region发送电子邮件
< span class =code-keyword> protected void btnSendmail_Click( object sender,EventArgs e )
{
// System.Web.Mail.SmtpMail.SmtpServer在2.0中已过时/ span>
// System.Net.Mail.SmtpClient是2.0中的替代类
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();

尝试
{
MailAddress fromAddress = new MailAddress(txtEmail.Text,txtName.Text);

// 您可以指定服务器的主机名或ipaddress
// IIS中的默认值为localhost \
smtpClient.Host = < span class =code-string>
smtp.gmail.com;
// smtpClient.Host =localhost;

// 默认端口为25
smtpClient.Port = 25 ;

// 发件人地址将作为MailAddress对象提供
message.From = fromAddress;

// 解决MailAddress的收集
message.To .Add( karthikh87@gmail.com);
message.Subject = 反馈;

// CC和BCC可选
// MailAddressCollection类用于将电子邮件发送给各个用户
// 您可以将地址指定为新邮件地址(admin1@yoursite.com)
message.CC.Add( karthikh87@gmail.com);
message.CC.Add( karthikh87@gmail.com);

// 您可以直接将地址指定为字符串
消息.Bcc.Add( new MailAddress( karthikh87 @ gmail .COM));
message.Bcc.Add( new MailAddress( karthikh87@gmail.com));

// 正文可以是Html或文本格式
< span class =code-comment> //
如果是html消息则指定true
message.IsBodyHtml = ;

// 邮件正文内容
message.Body = txtMessage 。文本;

// 发送SMTP邮件
smtpClient.Send(消息);

lblStatus.Text = 电子邮件已成功发送。;
}
catch (例外情况)
{
lblStatus.Text = 发送电子邮件失败。< br> + ex.Message;
}
Response.Redirect( index.html);
}
#endregion

#region重置
受保护 void btnReset_Click( object sender,EventArgs e)
{
txtName.Text = ;
txtMessage.Text = ;
txtEmail.Text = ;
}
#endregion
}

解决方案

您必须添加以下代码才能尽可能简单地发送邮件...



[给出了gmail帐户的示例。

这意味着您必须拥有一个Gmail帐户才能将邮件发送到任何地址。]



  string  pweda =   FromMailPassword;  //  (ConfigurationManager.AppSettings [password]);  
string from = FromYourmail@gmail.com; // 将其替换为您自己的正确Gmail地址
string to = abc@gef.com; // 将其替换为您要向其发送邮件的电子邮件地址
系统。 Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress( from );
mail.Subject = 这是一个测试邮件;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = 测试邮件。;

mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();

// 添加Creddentials-使用您自己的电子邮件ID和密码
client.UseDefaultCredentials = false ;
client.Credentials = new System.Net.NetworkCredential( from ,pweda);
client.Port = 587 ; // Gmail适用于此端口
client.Host = smtp.gmail.com;
client.EnableSsl = true ; // Gmail适用于服务器安全层

尝试
{
client.Send(mail);
Response.Write( Message Sent ...);
}
catch (例外情况)
{
Exception ex2 = ex;
string errorMessage = string .Empty;
while (ex2!= null
{
errorMessage + = ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage);
} // 结束尝试





如果您有任何其他信息,您可以从任何邮件地址发送...

只需更换您可以轻松获得的凭据,端口和主机名称谷歌。



您可以通过为'mail.Body'赋值来发送任何内容。



添加此项代码,你想要发送邮件...



快乐编码........


查看我的回答这里

错误用于发送电子邮件的asp.net代码 [ ^ ]



谢谢

--RA

<试试这个

使用ASP.net通过Gmail帐户发送电子邮件(Gmail SMTP服务器帐户) [ ^ ]

< br $>
缺少---- System.Net.NetworkCredential(来自密码)


// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost \
// smtpClient.Host = "smtp.google.com";

currently i am using : smtpClient.Host = "localhost";

i get mail to gmail id but it also gets error as fake email id report r learn more so if i don't want to get that what i have to do instead of "local host"


from where i get hostname..??
which ip address i have to give gmail or hosting providers..??
and why i get that error "This message may not have been sent by: test@gmail.com Learn more Report phishing"..??

Check code below:

using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page 
{
    #region  "Send email"
    protected void btnSendmail_Click(object sender, EventArgs e)
    {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost \
            smtpClient.Host = "smtp.gmail.com";
           // smtpClient.Host = "localhost";

            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("karthikh87@gmail.com");
            message.Subject = "Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
            message.CC.Add("karthikh87@gmail.com");
            message.CC.Add("karthikh87@gmail.com");

            // You can specify Address directly as string
            message.Bcc.Add(new MailAddress("karthikh87@gmail.com"));
            message.Bcc.Add(new MailAddress("karthikh87@gmail.com"));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = false;

            // Message body content
            message.Body = txtMessage.Text;
         
            // Send SMTP mail
            smtpClient.Send(message);

            lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
        }
        Response.Redirect("index.html");
    }
    #endregion

    #region "Reset"
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtMessage.Text = "";
        txtEmail.Text = "";
    }
    #endregion
}

解决方案

You have to add the following codes to send a mail as simple as possible...

[Example is given for gmail account.
That means you have to have one gmail account to send a mail to any address.]

string pweda = "FromMailPassword"; //(ConfigurationManager.AppSettings["password"]);
        string from = "FromYourmail@gmail.com"; //Replace this with your own correct Gmail Address
        string to = "abc@gef.com"; //Replace this with the Email Address to whom you want to send the mail
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = "This is a test mail";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "Test Mail.";
 
        mail.Priority = MailPriority.High;
        SmtpClient client = new SmtpClient();
 
        //Add the Creddentials- use your own email id and password
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(from, pweda);
        client.Port = 587; // Gmail works on this port
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true; //Gmail works on Server Secured Layer

        try
        {
            client.Send(mail);
            Response.Write("Message Sent...");
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            HttpContext.Current.Response.Write(errorMessage);
        } // end try



You can send from any mail address if you have any other...
Just replace with the credentials and port and host names which you can get easily get from google.

You can send anything by assigning value to 'mail.Body'.

Add this code where ever you want send the mail...

Happy coding........


See my Answer Here
error in asp.net code to send email[^]

Thanks
--RA


Try this
Sending E-mail using ASP.net through Gmail account (Gmail SMTP Server account)[^]

Missing ----System.Net.NetworkCredential(from, "Password")


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

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