问题描述
我是asp.net和C#的新手,我想问你一些事情.
I am new to asp.net and c# and i want to ask you something .
我已经创建了联系表单,但是我用于发送电子邮件的代码需要用户提供其电子邮件密码.这是代码:
I have created a contact form but the code i use for sending emails needs the user to give his email password . This is the code :
var toAddress = new MailAddress("[email protected]","Name");
var fromAddress = new MailAddress(EmailAddress.Text,Name.Text);
const string fromPassword ="PASS"; //发送联系表单电子邮件密码的用户
var subject ="SUBJECT";
//
字符串fileName = Server.MapPath(〜/App_Data/ContactForm.txt");
字符串mailBody = File.ReadAllText(fileName);
mailBody = mailBody.Replace(" ## Name ##" ;, Name.Text);
mailBody = mailBody.Replace("## Email ##",EmailAddress.Text);
mailBody = mailBody.Replace(" ## HomePhone ##",PhoneHome.Text);
mailBody = mailBody.Replace("## BusinessPhone ##",PhoneBusiness.Text);
mailBody = mailBody.Replace(" ## Comments ##" ;, Comments.Text);
//
var smtp =新的SmtpClient
{
主持人="smtp.gmail.com",
端口= 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
凭据=新的NetworkCredential(fromAddress.Address,fromPassword)
};
使用(var message = new MailMessage(fromAddress,toAddress)
{
主题=主题,
正文= mailBody
})
{
smtp.Send(message);
}
var toAddress = new MailAddress("[email protected]", "Name");
var fromAddress = new MailAddress(EmailAddress.Text, Name.Text);
const string fromPassword = "PASS"; // user who sends contact form email password
var subject = "SUBJECT";
//
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = File.ReadAllText(fileName);
mailBody = mailBody.Replace("##Name##", Name.Text);
mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
mailBody = mailBody.Replace("##BusinessPhone##", PhoneBusiness.Text);
mailBody = mailBody.Replace("##Comments##", Comments.Text);
//
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = mailBody
})
{
smtp.Send(message);
}
有人知道如何在用户不提供电子邮件密码的情况下发送电子邮件吗?
Does anyone know how to implement to send email without the user giving his email password ??
推荐答案
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = "smtp.gmail.com";
smtpclient.Port = 25;
smtpclient.Credentials = new NetworkCredential("[email protected]", "XYZ");
smtpclient.EnableSsl = true;
MailMessage objmsg = new MailMessage();
objmsg.From = new MailAddress("[email protected]");
objmsg.To.Add("[email protected]");
objmsg.Subject = "Test mail";
objmsg.Body = "test mail";
objmsg.IsBodyHtml = true;
smtpclient.Send(objmsg);
这篇关于联络表格电邮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!