如何在ASP.NET中联系我们页面发送邮件? [英] How do send a mail in contact us page in ASP.NET?

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

问题描述

姓名:aaa

Mobileno:<删除>

Emailaddress:name@gmail.com

评论:hai

字段在那里,现在我想将此详细信息发送到mycompany电子邮件地址。可以提前感谢。但mycompany在Emailaddress领域收到邮件......



我尝试过:



收件箱

在name@gmail.com接收邮件

解决方案

网上有很多很多教程和例子。这是一个, [ ^ ]



您将使用System.Net .Mail命名空间, SmtpClient类(System.Net。邮件) [ ^ ]。


你可以试试这个,



 尝试 
{
var SendersAddress = senderemail@gmail.com;
var SendersPassword = senderpassword;
var ccMailId = ccemail @ gmail。 COM;
var ReceiversAddress = name @ gmail。 COM;
var subject = Deatils;
var body = 名称:aaa, Mobileno:9786543210,Emailaddress:name @ gmail.com,评论:hai;
// 我们将使用Smtp客户端,它允许我们使用SMTP协议发送电子邮件
// 我在{}
// gmails smtp服务器名称为smtp.gmail.com,端口号为587
SmtpClient smtp = < span class =code-keyword> new
SmtpClient
{
Host = smtp.gmail.com
Port = 587
EnableSsl = true
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkC redential(SendersAddress,SendersPassword),
Timeout = 3000000
};

// MailMessage代表邮件消息
// 它是4个参数(From,TO,subject,body)

MailMessage消息= new MailMessage(SendersAddress,ReceiversAddress,subject,body);
/ * 我们使用上面指定的smtp服务器发送消息(MailMessage消息)* /

if (!string.IsNullOrEmpty(ccMailId))
{
message.CC.Add(ccMailId) ;
}

message.BodyEncoding = Encoding.UTF8;
message.SubjectEncoding = Encoding.UTF8;

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body);
htmlView.ContentType = new System.Net.Mime.ContentType( text / html的);
message.AlternateViews.Add(htmlView);

smtp.Send(message);
Console.WriteLine( 消息已成功发送);
// Console.ReadKey();
}
< span class =code-keyword> catch
(Exception ex)
{
Console.WriteLine(ex.Message);
// Console.ReadKey();
}


Name:aaa
Mobileno:<removed>
Emailaddress:name@gmail.com
Comments:hai
fields are there, now I want to sent this details to mycompany email address. It is possible thanks in advance. But mycompany received mail in Emailaddress field...

What I have tried:

Inbox
Receive mail in name@gmail.com

解决方案

There are many, many tutorials and examples online. Here is one, [^]

You'll use the System.Net.Mail namespace, SmtpClient Class (System.Net.Mail)[^].


You can try this,

try
            {
                var SendersAddress = "senderemail@gmail.com";
                var SendersPassword = "senderpassword";
                var ccMailId = "ccemail@gmail.com";
                var ReceiversAddress = "name@gmail.com";
                var subject = "Deatils";
                var body = "Name:aaa, Mobileno:9786543210, Emailaddress:name@gmail.com,Comments:hai";
                //we will use Smtp client which allows us to send email using SMTP Protocol
                //i have specified the properties of SmtpClient smtp within{}
                //gmails smtp server name is smtp.gmail.com and port number is 587
                SmtpClient smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new NetworkCredential(SendersAddress, SendersPassword),
                    Timeout = 3000000
                };

                //MailMessage represents a mail message
                //it is 4 parameters(From,TO,subject,body)

                MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
                /*WE use smtp sever we specified above to send the message(MailMessage message)*/

                if (!string.IsNullOrEmpty(ccMailId))
                {
                    message.CC.Add(ccMailId);
                }

                message.BodyEncoding = Encoding.UTF8;
                message.SubjectEncoding = Encoding.UTF8;

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body);
                htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
                message.AlternateViews.Add(htmlView);

                smtp.Send(message);
                Console.WriteLine("Message Sent Successfully");
                //Console.ReadKey();
            }
            catch (Exception ex)
            {                
                Console.WriteLine(ex.Message);
                //  Console.ReadKey();
            }


这篇关于如何在ASP.NET中联系我们页面发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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