1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
try{
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
smtpclient.EnableSsl = true;
// Create MailMessage
MailMessage message = new MailMessage(
"xxxxx@gmail.com",
"xxxxxx@gmail.com",
"test",
"hello");
MemoryStream memoryStream = new MemoryStream();
MemoryStream memoryStreamDest = new MemoryStream();
byte[] contentAsBytes = Encoding.UTF8.GetBytes(MyStringBuilder.ToString());
memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
// Set the position to the beginning of the stream.
memoryStream.Seek(0, SeekOrigin.Begin);
GZipStream gzCompressed;
byte[] bufferWrite = new byte[memoryStream.Length];
gzCompressed = new GZipStream(memoryStreamDest, CompressionMode.Compress, true);
memoryStream.Read(bufferWrite, 0, bufferWrite.Length);
gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);
gzCompressed.Close();
// Create attachment
ContentType contentType = new ContentType();
contentType.MediaType = MediaTypeNames.Application.Zip;
contentType.Name = "test.zip";
Attachment attachment = new Attachment(memoryStreamDest, contentType);
// Add the attachment
message.Attachments.Add(attachment);
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new NetworkCredential("xxxxx", "yyyyy");
// Send Mail via SmtpClient
smtpclient.Send(message); |
Partager