我尝试通过Gmail发送邮件,但使用下面的代码我得到了这个错误 [英] i tried send the mail through gmail but while use this below code i got this error
问题描述
Error 1 'MailPriority' is an ambiguous reference between 'System.Web.Mail.MailPriority' and 'System.Net.Mail.MailPriority' E:\E-mobileshop\admin\customerdetails.aspx.cs 223 112 E:\E-mobileshop\
cod:
protected void SendEmailWithAttachment(object sender,EventArgs e)
{
SendEmail(txtTo.Text.Trim(),,, txtSubject.Text.Trim(),txtBody.Text.Trim(),MailPriority.High,false);
}
private void SendEmail(string toAddress,string ccAddress,string bccAddress,string subject,string body,MailPriority priority,bool isHtml)
{
try
{
使用(SmtpClient smtpClient = new SmtpClient())
{
using(MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress(ramji.kid@gmail.com,ramji.kid,gmail。 Com);
//您可以指定服务器的主机名或ipaddress
smtpClient.Host =smtp.gmail。 COM; //你也可以在这里指定邮件服务器的IP地址
//默认端口是25
smtpClient.Port = 25;
NetworkCredential info = new NetworkCredential(ramji.kid@gmail.com,r);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network ;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//来自地址将作为MailAddress对象给出
message.From = fromAddress;
message.Priority = priority;
//解决MailAddress的集合
message.To.Add(txt_receiver.Text);
message.Subject = txt_subject.Text;
if(ccAddress.Length> ; 0)
{
message.CC.Add(ccAddress);
}
if(bccAddress.Length > 0)
{
message.Bcc.Add(bccAddress);
}
//正文可以是Html或文本格式
//如果是html消息则指定true
message.IsBodyHtml = isHtml;
//消息正文内容
message.Body = txt_body.Text;
//添加附件(如果有的话)
if(FileUpload1.PostedFile.ContentLength> 0)
{
附件附件=新附件(路径。 GetFullPath(FileUpload1.PostedFile.FileName));
message.Attachments.Add(attachment);
}
//发送SMTP邮件
smtpClient.Send (消息);
lblMessage.Text =发送到+ toAddress +的电子邮件成功!;
}
}
}
catch(例外ee)
{
lblMessage.Text = ee。 ToString();
}
}
你可以帮我纠正这个错误我在等你的
cod :
protected void SendEmailWithAttachment(object sender, EventArgs e)
{
SendEmail(txtTo.Text.Trim(), "", "", txtSubject.Text.Trim(), txtBody.Text.Trim(), MailPriority.High, false);
}
private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
{
try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress("ramji.kid@gmail.com", "ramji.kid, gmail.Com");
// You can specify the host name or ipaddress of your server
smtpClient.Host = "smtp.gmail.com"; //you can also specify mail server IP address here
//Default port will be 25
smtpClient.Port = 25;
NetworkCredential info = new NetworkCredential("ramji.kid@gmail.com", "r");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//From address will be given as a MailAddress Object
message.From = fromAddress;
message.Priority = priority;
// To address collection of MailAddress
message.To.Add(txt_receiver.Text);
message.Subject = txt_subject.Text;
if (ccAddress.Length > 0)
{
message.CC.Add(ccAddress);
}
if (bccAddress.Length > 0)
{
message.Bcc.Add(bccAddress);
}
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = isHtml;
// Message body content
message.Body = txt_body.Text;
// Add the attachment, if any
if (FileUpload1.PostedFile.ContentLength > 0)
{
Attachment attachment = new Attachment(Path.GetFullPath(FileUpload1.PostedFile.FileName));
message.Attachments.Add(attachment);
}
// Send SMTP mail
smtpClient.Send(message);
lblMessage.Text = "Email sent to " + toAddress + " successfully !";
}
}
}
catch (Exception ee)
{
lblMessage.Text = ee.ToString();
}
}
can u please help me to rectify this error i am waiting for u
推荐答案
你好请试试下面的代码< br $>
Hi Please try this code below
private void button1_Click(object sender, RoutedEventArgs e)
{
string footer = "If you have any questions about appointment cancelation,please do not hesitate to call your clinic front desk";
string subject = "Appointment Canceled";
string body = "Hi" + " " + "google" + "\n\n" + footer;
MailMessage message = new MailMessage();
message.From = new MailAddress("Ngqandu.Anele@gmail.com");
message.To.Add(new MailAddress("Anele.Ngqandu@live.nmmu.ac.za"));//sending to my nmmu live account
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("account@gmail.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(message);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
投票如果有帮助......
Vote if it helps...
基本上,在同一代码文件中有两个using语句引用不同的程序集:
Basically, you have two using statements in the same code file which refer to different assemblies:
using System.Web.Mail;
和
and
using System.Net.Mail;
并且两个程序集都包含相同的名称: MailPriority
。系统无法分辨你的意思,所以你有两种选择:
1)使用语句摆脱其中一个。
2)通过完整的引用名称前缀 MailPriority
明确指定您的意思。
And both assemblies contain the same name: MailPriority
. The system cannot tell which one you mean, so you have two alternatives:
1) Get rid of one of the using
statements.
2) Explicitly specify which you mean by prefixing MailPriority
by the full reference name.
这篇关于我尝试通过Gmail发送邮件,但使用下面的代码我得到了这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!