如何发送带或不带附件的电子邮件? [英] How to send email with or without attachment ?
本文介绍了如何发送带或不带附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做下面的代码......当我选择附件文件发送电子邮件然后它成功转到目的地.........但是当我没有选择附件文件发送电子邮件然后它显示成功消息..但实际上没有收到邮件.......如何解决它.......提前谢谢................
I am doing below code......When I choose attachment file for sending email then it goes to destination successfully.........But when I was not selected attachment file for sending email Then it display Successfully message..but actually did not got the mail.......How to solve it.......Thanks in advance................
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("Username", "Password");
MailMessage msg = new MailMessage();
msg.To.Add(txtto.Text);
msg.From = new MailAddress("@Username");
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;
if (!string.IsNullOrEmpty(txtattachment.Text))
{
Attachment data = new Attachment(txtattachment.Text);
msg.Attachments.Add(data);
}
client.Send(msg);
MessageBox.Show("successfully send.....");
推荐答案
参考:使用或不带附件在C#中发送电子邮件:通用例程。 [ ^ ]
附加文件..
Attaching document..
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Globalization;
string to = "Toaddress@gmail.com";//To address
string from = "From_adress@gmail.com";//From address
MailMessage message = new MailMessage(from, to);
if (fuAttachment.HasFile)//Attaching document
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
message .Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
message.Subject = txt_subject.text;
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}
这篇关于如何发送带或不带附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文