csharp:receive and send email c#
https://www.codeproject.com/Articles/3991/SMTP-and-POP3-Mail-Server
https://github.com/hanswolff/simple.mailserver
https://github.com/jstedfast/MimeKit
https://sourceforge.net/projects/hpop/files/2.0.5/
https://github.com/foens/hpop
http://www.joshwright.com/tips/Sending-Receiving-Email-in-CSharp
https://www.codeproject.com/articles/6062/a-pop-client-in-c-net
https://www.codeproject.com/articles/456380/a-csharp-smtp-server-receiver
http://csharpmail.codeplex.com/
http://higlabo.codeplex.com/
https://github.com/higty/higlabo
http://www.codeproject.com/Articles/399207/Understanding-the-insides-of-the-SMTP-Mail-protoco
http://www.codeproject.com/Articles/404066/Understanding-the-insides-of-the-POP3-mail-protoco
http://www.codeproject.com/Articles/411018/Understanding-the-insides-of-the-IMAP-mail-protoco
http://stackoverflow.com/questions/236381/integrating-pop3-client-functionality-into-a-c-sharp-application
https://github.com/jstedfast/MailKit
https://github.com/andyedinborough/aenetmail
https://code.msdn.microsoft.com/windowsdesktop/Simple-IMAP-CLIENT-b249d2e6
http://stackoverflow.com/questions/44383/reading-email-using-pop3-in-c-sharp
http://dotnetctrlext.sourceforge.net/smtpop.htm
https://sourceforge.net/projects/dotnetctrlext/files/smtpop.net/
https://www.codeproject.com/articles/15611/pop-email-client-with-full-mime-support-net
https://sourceforge.net/projects/btnet/files/
http://ndoc.sourceforge.net/
http://nantcontrib.sourceforge.net/
http://nant.sourceforge.net/
https://en.wikipedia.org/wiki/List_of_object-relational_mapping_software#.NET
winform HTML EDIT:
https://zetahtmleditcontrol.codeplex.com/
https://www.codeproject.com/Articles/43954/ZetaHtmlEditControl
https://github.com/UweKeim/ZetaHtmlEditControl
https://netrix.codeplex.com/
https://github.com/jacobslusser/ScintillaNET
https://scintillanet.codeplex.com/SourceControl/latest
http://ribbon.codeplex.com/
https://officeribbon.codeplex.com/
https://github.com/shintadono/System.Windows.Forms.Ribbon
https://www.codeproject.com/Articles/23199/WPF-C-Ribbon-Control-Library
https://windowsribbon.codeplex.com/SourceControl/latest
https://fluent.codeplex.com/
https://github.com/fluentribbon/Fluent.Ribbon
https://github.com/joergkrause/netrix
https://github.com/joergkrause/netrix/tree/master/Assemblies/en
https://smithhtmleditor.codeplex.com/
https://github.com/textcontrol/
https://www.codeproject.com/articles/15559/a-windows-forms-based-text-editor-with-html-output
https://www.codeproject.com/Articles/30936/Using-ICSharpCode-TextEditor
http://www.icsharpcode.net/OpenSource/SD/Download/Default.aspx (C#开发IDE,国际化)
And the corresponding section in web.config:
<appSettings>
<add key="mailCfg" value="yourmail@example.com"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="yourmail@example.com">
<network defaultCredentials="false" host="mail.exapmple.com" userName="yourmail@example.com" password="your_password" port="25"/>
</smtp>
</mailSettings>
</system.net>
public static string SendEmail(string To, string Subject, string Msg, bool bodyHtml = false, bool test = false, Stream AttachmentStream = null, string AttachmentType = null, string AttachmentFileName = null)
{
try
{
System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
newMsg.BodyEncoding = System.Text.Encoding.UTF8;
newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
disposition.FileName = AttachmentFileName;
disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
newMsg.Attachments.Add(attachment);
}
if (test)
{
smtpClient.PickupDirectoryLocation = "C:\\TestEmail";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
}
else
{
//smtpClient.EnableSsl = true;
}
newMsg.IsBodyHtml = bodyHtml;
smtpClient.Send(newMsg);
return SENT_OK;
}
catch (Exception ex)
{
return "Error: " + ex.Message
+ "<br/><br/>Inner Exception: "
+ ex.InnerException;
}
}
/// <summary>
///
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public byte[] streamToByteArraydu(Stream stream)
{
byte[] byteArray = new byte[16 * 1024];
using (MemoryStream mSteram = new MemoryStream())
{
int bit;
while ((bit = stream.Read(byteArray, 0, byteArray.Length)) > 0)
{
mSteram.Write(byteArray, 0, bit);
}
return mSteram.ToArray();
}
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public byte[] StreamToByteArray(string fileName)
{
byte[] total_stream = new byte[0];
using (Stream input = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
byte[] stream_array = new byte[0];
// Setup whatever read size you want (small here for testing)
byte[] buffer = new byte[32];// * 1024];
int read = 0;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
stream_array = new byte[total_stream.Length + read];
total_stream.CopyTo(stream_array, 0);
Array.Copy(buffer, 0, stream_array, total_stream.Length, read);
total_stream = stream_array;
}
}
return total_stream;
}
/// <summary>
///
/// </summary>
/// <param name="instream"></param>
/// <returns></returns>
//public static byte[] ReadAllBytes(this Stream instream)
//{
// if (instream is MemoryStream)
// return ((MemoryStream)instream).ToArray();
// using (var memoryStream = new MemoryStream())
// {
// instream.CopyTo(memoryStream); //CopyStream
// return memoryStream.ToArray();
// }
//}
//public static Stream ToStream(byte[] buffer)
//{
// Stream s = yourStream;
// int streamEnd = Convert.ToInt32(s.Length);
// buffer = new byte[streamEnd]; // byte[] buffer
// s.Read(buffer, 0, streamEnd);
//}
/// <summary>
///
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public Byte[] ToByteArrays(Stream stream)
{
Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
Byte[] buffer = new Byte[length];
stream.Read(buffer, 0, length);
return buffer;
}
/// <summary>
///
/// </summary>
/// <param name="inputStream"></param>
/// <returns></returns>
byte[] StreamToByteArray(Stream inputStream)
{
if (!inputStream.CanRead)
{
throw new ArgumentException();
}
// This is optional
if (inputStream.CanSeek)
{
inputStream.Seek(0, SeekOrigin.Begin);
}
byte[] output = new byte[inputStream.Length];
int bytesRead = inputStream.Read(output, 0, output.Length);
//Debug.Assert(bytesRead == output.Length, "Bytes read from stream matches stream length");
return output;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Stream AttachmentStream=null;
MemoryStream theMemStream = new MemoryStream();
string file = @"C:\TestEmail\Top1000WorldBanks2014.pdf";
if(File.Exists(file))
{
//AttachmentStream = File.Open(file, FileMode.Open, FileAccess.Read);
byte[] filebyte = ReadFile(file);
theMemStream.Write(filebyte, 0, filebyte.Length);// = File.OpenRead(file);
theMemStream.Position = 0;
//theMemStream.Close();
// AttachmentStream = theMemStream;// BytesToStream(filebyte); //new MemoryStream(filebyte);// // BytesToStream(filebyte);// FileToStream(file);//
//AttachmentStream.CanTimeout =true;
//AttachmentStream.ReadTimeout = 1000;
//AttachmentStream.WriteTimeout = 1000;
//AttachmentStream = BytesToStream(filebyte);
//theMemStream.Close();
}
bool issend = SendEmail("geovindu@163.com", "發送郵件測試geovindu", "<p> geovindu江蘇黃河</p>", true, true, theMemStream, "pdf", "2014.pdf");
theMemStream.Close();
if (issend)
{
MessageBox.Show("ok");
}
else
{
MessageBox.Show("no");
}
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
//fileStream.ReadTimeout = 1000;
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
//stream.CanTimeout=true;
//stream.ReadTimeout = 1000;
//stream.WriteTimeout = 1000;
return stream;
}
/// <summary>
/// no
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="output"></param>
public void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return buffer;
}
/// <summary>
///
/// </summary>
/// <param name="To"></param>
/// <param name="Subject"></param>
/// <param name="Msg"></param>
/// <param name="bodyHtml"></param>
/// <param name="AttachmentStream"></param>
/// <param name="AttachmentType"></param>
/// <param name="AttachmentFileName"></param>
/// <returns></returns>
public bool SendEmaildu(string To, string Subject, string Msg, bool bodyHtml, MemoryStream AttachmentStream, string AttachmentType, string AttachmentFileName)
{
bool SENT_OK = false;
try
{
System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
newMsg.BodyEncoding = System.Text.Encoding.UTF8;
newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
{
// ADD AN ATTACHMENT.
//String sFile = @"C:\Source\LNS_IS.TXT";
//MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
//string filePath = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls";
//MemoryStream memStream = new MemoryStream();
//using (FileStream fileStream = File.OpenRead(filePath))
//{
// memStream.SetLength(fileStream.Length);
// fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
//}
newMsg.Attachments.Add(new Attachment(AttachmentStream, AttachmentFileName)); //MediaTypeNames.Image.Jpeg)
//System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
//System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
//disposition.FileName = AttachmentFileName;
//disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
//newMsg.Attachments.Add(attachment);
}
newMsg.IsBodyHtml = bodyHtml;
SENT_OK = true;
smtpClient.Send(newMsg);
return SENT_OK;
}
catch (Exception ex)
{
ex.Message.ToString();
SENT_OK = false;
//"Error: " + ex.Message + "<br/><br/>Inner Exception: "+ ex.InnerException;
return SENT_OK;
}
}
/// <summary>
///
/// </summary>
/// <param name="To"></param>
/// <param name="Subject"></param>
/// <param name="Msg"></param>
/// <param name="bodyHtml"></param>
/// <param name="test"></param>
/// <param name="AttachmentStream"></param>
/// <param name="AttachmentType"></param>
/// <param name="AttachmentFileName"></param>
/// <returns></returns>
public static bool SendEmail(string To, string Subject, string Msg, bool bodyHtml, bool test, Stream AttachmentStream, string AttachmentType, string AttachmentFileName)
{
bool SENT_OK =false;
try
{
System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
newMsg.BodyEncoding = System.Text.Encoding.UTF8;
newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
{
// ADD AN ATTACHMENT.
//String sFile = @"C:\Source\LNS_IS.TXT";
//MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
string filePath = @"C:\TestEmail\SKYPE11月黑咭生日會員.xls";
MemoryStream memStream = new MemoryStream();
using (FileStream fileStream = File.OpenRead(filePath))
{
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}
newMsg.Attachments.Add(new Attachment(memStream, AttachmentFileName, MediaTypeNames.Image.Jpeg));
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
disposition.FileName = AttachmentFileName;
disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
newMsg.Attachments.Add(attachment);
}
if (test)
{
smtpClient.PickupDirectoryLocation = "C:\\TestEmail";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
}
else
{
//smtpClient.EnableSsl = true;
}
newMsg.IsBodyHtml = bodyHtml;
SENT_OK = true;
smtpClient.Send(newMsg);
return SENT_OK;
}
catch (Exception ex)
{
ex.Message.ToString();
SENT_OK = false;
//"Error: " + ex.Message + "<br/><br/>Inner Exception: "+ ex.InnerException;
return SENT_OK;
}
}
/*
data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据
*/
/// <summary>
///
/// </summary>
/// <param name="base64String"></param>
/// <returns></returns>
public Image Base64ToImage(string base64String)
{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
return image;
}
}
/// <summary>
///
/// </summary>
/// <param name="image"></param>
/// <param name="format"></param>
/// <returns></returns>
public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to base 64 string
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
浙公网安备 33010602011771号