C#控制台程序,发送邮件,可带附件
??? 最近幾天由于公司發送了大量內容相同的郵件,而被國外的反垃圾郵件組織列入了黑名單,致使很多客戶收不到我們的郵件,客服接到投訴,而之前做的一個查詢日志的小頁面,因為某種原因,訪問速度很慢,甚至這幾天人一多,頁面就總是超時.剛開始以為是程序問題或者是數據量比較大,但是程序也只是一句簡單的兩表聯查,兩張表大概也就200W左右的數據,在線下跑的時候速度很快,可是一到線上就慢了很多.后來到系統組那邊找了一下,發現是線上的機器在連數據庫的時候,沒有將數據庫的IP添加到Host里,添進去之后速度瞬間就提了上去.
??? 昨天在研究解決策略的時候,同事建議建立一個定時任務,將查詢的結果以附件的形式發送給查詢人,所以寫了一個控制臺程序來跑.因為以前沒有接觸過這方面的東西,所以查了很多,最后還是發現MSDN真是個好東西,呵呵,不說了,代碼帖上來,大家把***換成自己的郵件服務器,然后發件人和收件人相應地改一下,程序就可以跑了.有什么問題還請大家積極地反應,幫助小弟提高.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Net;
using System.Net.Mime;
using System.Net.Mail;
namespace SendEmail
{
??? class Program
??? {
??????? //smtp.UseDefaultCredentials = true;
??????? //client.Credentials = CredentialCache.DefaultNetworkCredentials;
??????? static void Main(string[] args)
??????? {
?????????
?????????? //DefaultSendEmail();
?????????? // WriteEmail();
??????????? Console.WriteLine(WriteEmail().ToString());
??????????? //CreateMessageWithAttachment();
??????? }
??????
??????? //Single receiver test.MSDN上最簡單的發送郵件
??????? public static void DefaultSendEmail(string server)//你的郵件服務器
??????? {
??????????? string to = "job.chang@live.cn";
??????????? string from = "job.chang@live.cn";
??????????? string subject = "Using the new SMTP client.";
??????????? string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
??????????? MailMessage message = new MailMessage(from, to, subject, body);
???????????
??????????? SmtpClient client = new SmtpClient(server);
??????????? //Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
??????????? //client.Timeout = 100;
??????????? // Credentials are necessary if the server requires the client
??????????? // to authenticate before it will send e-mail on the client's behalf.
??????????? client.Credentials = CredentialCache.DefaultNetworkCredentials;
??????????? client.Send(message);
??????????? Console.WriteLine("Email sended.");
??????? }
??????? //Multiply users test.
??????? public static string MultiSendEmail(string mailFrom,string mailTo,string mailCC,string mailHead,string mailBody,ArrayList mailAttach,bool isHtml)
??????? {
??????????? //改用XML文件在Web.Configure中配置.
??????????? string eServer = "***.**.**.**";//需要換成你的郵件服務器
??????????? int ePort = 25;//默認
??????????? MailMessage eMail = new MailMessage();
??????????? SmtpClient eClient = new SmtpClient(eServer,ePort);
??????????? eClient.UseDefaultCredentials = true;
??????????? eMail.Subject = mailHead;
??????????? eMail.SubjectEncoding = Encoding.UTF8;
???????????
??????????? eMail.Body = mailBody;
??????????? eMail.BodyEncoding = Encoding.UTF8;
??????????? eMail.From = new MailAddress(mailFrom);
??????????? //Receiver
??????????? string[] arrMailAddr;
??????????? try
??????????? {
???????????????
??????????????? //用";"分割多個收件人.
??????????????? eMail.To.Clear();
??????????????? if(!string.IsNullOrEmpty(mailTo))
??????????????? {
??????????????????? arrMailAddr=mailTo.Split(';');
??????????????????? foreach(string strTo in arrMailAddr)
??????????????????? {
??????????????????????? if(!string.IsNullOrEmpty(strTo))
??????????????????????? {
??????????????????????????? eMail.To.Add(strTo);
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? //用";"分割多個抄送人.
??????????????? eMail.CC.Clear();
??????????????? if(!string.IsNullOrEmpty(mailCC))
??????????????? {
??????????????????? arrMailAddr=mailCC.Split(';');
??????????????????? foreach(string strCC in arrMailAddr)
??????????????????? {
??????????????????????? if(!string.IsNullOrEmpty(strCC))
??????????????????????? {
??????????????????????????? eMail.CC.Add(strCC);
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? //用";"分割多個秘件抄送人.
??????????????? //eMail.Bcc.Clear();
??????????????? //if(!string.IsNullOrEmpty(mailBCC))
??????????????? //{
??????????????? //??? arrMailAddr=mailBCC.Split(';');
??????????????? //??? foreach(string strBCC in arrMailAddr)
??????????????? //??? {
??????????????? //??????? if(!string.IsNullOrEmpty(strBCC))
??????????????? //??????? {
??????????????? //??????????? eMail.Bcc.Add(strBCC);
??????????????? //??????? }
??????????????? //??? }
??????????????? //}
??????????????? if(isHtml)
??????????????? {
??????????????????? eMail.IsBodyHtml=true;
??????????????? }
??????????????? else
??????????????? {
??????????????????? eMail.IsBodyHtml=false;
??????????????? }
??????????????? //Attachment
??????????????? eMail.Attachments.Clear();
??????????????? if(mailAttach!=null)
??????????????? {
??????????????????? for(int i=0;i<mailAttach.Count;i++)
??????????????????? {
??????????????????????? if(!string.IsNullOrEmpty(mailAttach[i].ToString()))
??????????????????????? {
??????????????????????????? eMail.Attachments.Add(new Attachment(mailAttach[i].ToString()));
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? eClient.Send(eMail);
??????????????? return "郵件已發送,請查收!";
?
??????????? }
??????????? catch(Exception ex)
??????????? {
??????????????? return "郵件發送失敗,原因:"+ex.Message.ToString();
??????????? }
?
??????? }
??????? public static string strMailFrom;
??????? public static string strMailTo;
??????? public static string strMailCC;
??????? public static string strMailSubject;
??????? //public static string strStartTime;
??????? //public static string strEndTime;
??????? public static string strMailBody;
??????? public static ArrayList arrAttachment;
??????? public static string? WriteEmail()
??????? {
??????????? strMailFrom = "job.chang@live.cn";
??????????? Console.WriteLine("請輸入收件人郵箱,以"+";"+"間隔!");
??????????? strMailTo = Console.ReadLine().ToString();
??????????? strMailCC = "job.chang@live.cn";
??????????? strMailSubject = "Test Mail";
??????????? strMailBody = "Test Mail";
??????????? arrAttachment=new ArrayList ();
??????????? arrAttachment.Add(@"c:\cmd.txt");
??????????? arrAttachment.Add(@"c:\job.txt");
??????????? arrAttachment.Add(@"c:\t.txt");
???????????
??????????? return MultiSendEmail(strMailFrom,strMailTo,strMailCC,strMailSubject,strMailBody,arrAttachment ,false );
??????? }
??????? //微軟家自帶的發送帶附件.
??????? public static void CreateMessageWithAttachment(string server)
??????? {
??????????? // Specify the file to be attached and sent.
??????????? // This example assumes that a file named Data.xls exists in the
??????????? // current working directory.
??????????? string file = @"c:\t.txt";
??????????? // Create a message and set up the recipients.
??????????? MailMessage message = new MailMessage(
?????????????? "job.chang@live.cn",
?????????????? "job.chang@live.cn",
?????????????? "Quarterly data report.",
?????????????? "See the attached spreadsheet.");
??????????? // Create? the file attachment for this e-mail message.
??????????? Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
??????????? // Add time stamp information for the file.
??????????? ContentDisposition disposition = data.ContentDisposition;
??????????? disposition.CreationDate = System.IO.File.GetCreationTime(file);
??????????? disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
??????????? disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
??????????? // Add the file attachment to this e-mail message.
??????????? message.Attachments.Add(data);
??????????? //Send the message.
??????????? SmtpClient client = new SmtpClient(server);
??????????? // Add credentials if the SMTP server requires them.
??????????? client.Credentials = CredentialCache.DefaultNetworkCredentials;
??????????? client.Send(message);
??????????? // Display the values in the ContentDisposition for the attachment.
??????????? ContentDisposition cd = data.ContentDisposition;
??????????? Console.WriteLine("Content disposition");
??????????? Console.WriteLine(cd.ToString());
??????????? Console.WriteLine("File {0}", cd.FileName);
??????????? Console.WriteLine("Size {0}", cd.Size);
??????????? Console.WriteLine("Creation {0}", cd.CreationDate);
??????????? Console.WriteLine("Modification {0}", cd.ModificationDate);
??????????? Console.WriteLine("Read {0}", cd.ReadDate);
??????????? Console.WriteLine("Inline {0}", cd.Inline);
??????????? Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
??????????? foreach (DictionaryEntry d in cd.Parameters)
??????????? {
??????????????? Console.WriteLine("{0} = {1}", d.Key, d.Value);
??????????? }
??????????? data.Dispose();
??????? }
??? }
}
轉載于:https://www.cnblogs.com/zhangkun-w/archive/2008/12/05/1348518.html
總結
以上是生活随笔為你收集整理的C#控制台程序,发送邮件,可带附件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mongodb集群与分片的配置说明
- 下一篇: OpenDDS安装