Anda di halaman 1dari 3

Sending Mail using C# - More mail sending code, and its not even mine!

Heres an interesting free snippet of code I just used to send mail from my .n... http://www.osix.net/modules/article/?id=513

26489 total geeks with 3501 solutions


Recent challengers:
djcomidi bonus 11 - 05:44PM
djcomidi bonus 17 - 04:43PM
taurean1434 level 3 - 11:07AM

Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address

Articles Article viewer


C
Dot.Net
Windows Sending Mail using C#
Visual Basic
Written by: bb
Security
Miscellaneous Published by: bb
Published on: 2004-04-28 15:07:33 Search OSI about Dot.Net. More articles by bb.
Show/Hide...
Topic: Dot.Net viewed 19795 times send this article printer friendly

GEEK Digg this!


Rate this article :
Challenge
Bonus Levels More mail sending code, and its not even mine! Heres an interesting free snippet of code I just used to send mail from my .net application. Why write your own rather than use the System.Web.Mail namespace? easy .. its rubbish!

Geek Reverser
View Solutions
Test Centre
Mau Ke Luar Angkasa ?
Hall of Brain www.axeapollo.co.id
Hall of Bonus Tinggal Hari Ini Kesempatan Lo Bisa Pergi Ke Luar Angkasa Sob. Buruan!
Hall of
Reverse The article including the code is here
Geek or Freak
The code library is here

User's box And just to fill up our database and think12's quotas .. here is the code
Username: using System;
using System.Text;
Password: using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Web.Mail;
namespace SMTP
Forgot
{
password?
/// <summary>
New account /// provides methods to send email via smtp direct to mail server
/// </summary>
Shoutbox public class SmtpDirect
{
MaxMouse
/// <summary>
It's Friday... /// Get / Set the name of the SMTP mail server
That's good /// </summary>
enough for me! public static string SmtpServer;
CodeX private enum SMTPResponse: int
non stop lolz {
here but thats CONNECT_SUCCESS = 220,
soon to end GENERIC_SUCCESS = 250,
thanks to uni, DATA_SUCCESS = 354,
surely the rest QUIT_SUCCESS = 221
}
of the world is
public static bool Send(MailMessage message)
going good?
{
stabat
IPHostEntry IPhst = Dns.Resolve(SmtpServer);
how things are IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
going guys? Socket s= new Socket(endPt.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
Here... s.Connect(endPt);
boring...
CodeX if(!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))
I must be {
going wrong on s.Close();
the password return false;
lengths then, }
as long as it
Senddata(s, string.Format("HELO {0}rn", Dns.GetHostName() ));
was done on
if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
ECB {
MaxMouse s.Close();
lol... the key is return false;
in hex (MD5: }
of the string
"doit" without Senddata(s, string.Format("MAIL From: {0}rn", message.From ));
the "'s) and is if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
in lower case. {
Maybe i should
s.Close();
have
return false;
submitted this
}
as a challenge!
string _To = message.To;
Donate string[] Tos= _To.Split(new char[] {';'});
foreach (string To in Tos)
Donate and help {
us fund new Senddata(s, string.Format("RCPT TO: {0}rn", To));
challenges if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
{
s.Close();
return false;
Due Date: Sep 30
}
September
Goal:
$40.00 }

Gro ss: $0.00 if(message.Cc!=null)


Net {
Balance: $0.00 Tos= message.Cc.Split(new char[] {';'});
Le ft to go: $40.00 foreach (string To in Tos)
{
Contributors
Senddata(s, string.Format("RCPT TO: {0}rn", To));
if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
{
s.Close();
News Feeds
return false;
The Register }
Memory muddle }
muddies Intel"s }
Exascale ambitions
Microsoft reissues StringBuilder Header=new StringBuilder();
September patches Header.Append("From: " + message.From + "rn");
after user Tos= message.To.Split(new char[] {';'});
complaints
Header.Append("To: ");
for( int i=0; i< Tos.Length; i++)

1 of 3 9/15/2013 10:50 AM
Sending Mail using C# - More mail sending code, and its not even mine! Heres an interesting free snippet of code I just used to send mail from my .n... http://www.osix.net/modules/article/?id=513

Server hack heads {


up the stack for a
Header.Append( i > 0 ? "," : "" );
new challenge
Header.Append(Tos[i]);
Billio naire
}
engineer Ray
Dolby, Header.Append("rn");
80 , dies at ho me in if(message.Cc!=null)
San Francisco {
The future o f PCIe: Tos= message.Cc.Split(new char[] {';'});
Get small, speed Header.Append("Cc: ");
up, think o utside for( int i=0; i< Tos.Length; i++)
the box {
Teen buys Header.Append( i > 0 ? "," : "" );
WikiLeaks Header.Append(Tos[i]);
server fo r $3 3,00 0 }
? w ith dad"s eBay
Header.Append("rn");
account
}
What"s in it for
Header.Append( "Date: " );
server buyers now
that Intel"s Xeo n Header.Append(DateTime.Now.ToString("ddd, d M y H:m:s z" ));
E5 -260 0 v2 is Header.Append("rn");
here? Header.Append("Subject: " + message.Subject+ "rn");
Couchbase relaxes Header.Append( "X-Mailer: SMTPDirect v1rn" );
NoSQL derrire string MsgBody = message.Body;
into if(!MsgBody.EndsWith("rn"))
mo bile seats MsgBody+="rn";
It"s about tim e: if(message.Attachments.Count>0)
Java update {
includes to ol for Header.Append( "MIME-Version: 1.0rn" );
blocking drive-by Header.Append( "Content-Type: multipart/mixed; boundary=unique-boundary-1rn" );
exploits
Header.Append("rn");
Happy Friday the
Header.Append( "This is a multi-part message in MIME format.rn" );
13 th! It"s
StringBuilder sb = new StringBuilder();
Pro gramm ers" Day
sb.Append("--unique-boundary-1rn");
Slashdot sb.Append("Content-Type: text/plainrn");
Study: Our 3 D sb.Append("Content-Transfer-Encoding: 7Bitrn");
Universe C ould
sb.Append("rn");
Have
Originate d From a
sb.Append(MsgBody + "rn");
4D Black Ho le sb.Append("rn");
Ask Slashdot:
Attracting foreach(object o in message.Attachments)
Developers To {
Abando nware? MailAttachment a = o as MailAttachment;
Stephen Colbert byte[] binaryData;
and if(a!=null)
the Monste r Truck {
of Tivos FileInfo f = new FileInfo(a.Filename);
Open Source, Open sb.Append("--unique-boundary-1rn");
Wo rld sb.Append("Content-Type: application/octet-stream; file=" + f.Name + "rn");
How Amateurs sb.Append("Content-Transfer-Encoding: base64rn");
Destro yed the sb.Append("Content-Disposition: attachment; filename=" + f.Name + "rn");
Pro fessio nal Music
sb.Append("rn");
Business
FileStream fs = f.OpenRead();
US, Russia Agree
binaryData = new Byte[fs.Length];
On
Plan To Dispose of
long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
Syria"s C hemical fs.Close();
Weapons string base64String = System.Convert.ToBase64String(binaryData, 0,binaryData.Length);
The Boy Genius of
Ulan Bator for(int i=0; i< base64String.Length ; )
Japan Controls {
Ro cket Launch int nextchunk=100;
With if(base64String.Length - (i + nextchunk ) <0)
Just 8 People and nextchunk = base64String.Length -i;
2 sb.Append(base64String.Substring(i, nextchunk));
Laptops sb.Append("rn");
45 % of U.S. Jobs i+=nextchunk;
Vulnerable To
}
Autom ation
sb.Append("rn");
Dogs Lo ve Ro bots,
}
Prefer Hum ans
}
MsgBody=sb.ToString();
}

Senddata(s, ("DATArn"));
if(!Check_Response(s, SMTPResponse.DATA_SUCCESS))
{
s.Close();
return false;
}
Header.Append( "rn" );
Header.Append( MsgBody );
Header.Append( ".rn" );
Header.Append( "rn" );
Header.Append( "rn" );
Senddata(s, Header.ToString());
if(!Check_Response(s, SMTPResponse.GENERIC_SUCCESS ))
{
s.Close();
return false;
}

Senddata(s, "QUITrn");
Check_Response(s, SMTPResponse.QUIT_SUCCESS );
s.Close();
return true;
}
private static void Senddata(Socket s, string msg)
{
byte[] _msg = Encoding.ASCII.GetBytes(msg);
s.Send(_msg , 0, _msg .Length, SocketFlags.None);
}
private static bool Check_Response(Socket s, SMTPResponse response_expected )
{
string sResponse;
int response;
byte[] bytes = new byte[1024];
while (s.Available==0)
{
System.Threading.Thread.Sleep(100);
}

s.Receive(bytes, 0, s.Available, SocketFlags.None);


sResponse = Encoding.ASCII.GetString(bytes);
response = Convert.ToInt32(sResponse.Substring(0,3));
if(response != (int)response_expected)
return false;
return true;
}
}
}

Did you like this article? There are hundreds more.


Join OSIX
Get Involved

2 of 3 9/15/2013 10:50 AM
Sending Mail using C# - More mail sending code, and its not even mine! Heres an interesting free snippet of code I just used to send mail from my .n... http://www.osix.net/modules/article/?id=513

Challenge or Test your skills

Comments:
bb oh, and if your writing a real system, dont bother using this - use OpenSmtp. A free C# mail library hosted on sourceforge.net
20 04-0 4-28
15 :10:09

think12 Nnnnooo... Not my quota's!


20 04-0 4-30
20 :41:14

Anonymous But what change has to be made to the above code, so that if I want to send my attachment as inline.
20 06-1 2-21
Help Needed.
07 :12:06 Thnx.

Anonymous hai
20 07-1 2-22
04 :17:48

neo182 Thanks for such great and easy to go tutorial!


20 08-1 1-26
10 :20:05

Library RFID Systems


www.fetechgroup.com.au
Self Serve Check Out - Self Returns Book Sorters, Stock Take, Payments

Anonymous Thanks for such great and easy to go tutorial!


20 09-0 5-29
10 :20:32

Download Free Software


mobogenie.com/download-software
Download Free PC Manager Software. Easy File Transfer. Download Now !

Anonymously add a comment: (or register here)


(registration is really fast and we send you no spam)

BB Code is enabled.
Captcha Number:

C# Code C# XML C# Java C# Tutorial

Blogs: (Peo ple who have posted blogs on this subject..)

bb ASP.NET RadioButton GroupName when inside a Repeater on Sun 10th Jun 8am
I was thankful on finding this nugget of code, which makes the groupname work out when slamming in radiobuttons in an asp.net repeater. http://www.codeguru.com/csharp/csharp/cs _controls/custom/article.php/c12371/

Your Ad Here

Copyright Open Source Institute, 2006

3 of 3 9/15/2013 10:50 AM

Anda mungkin juga menyukai