Example #1
0
        public static void Send(MailMessage message)
        {
            try {
                // wrap the MailMessage in a MailMessage wrapper for easier
                // access to properties and to add some functionality
                MailMessageWrapper messageWrapper = new MailMessageWrapper(message);

                SmtpClient smtp = new SmtpClient(smtpServer);

                smtp.Send(messageWrapper);

                smtp.Close();
            } catch (SmtpException ex) {
                // LAMESPEC:
                // .NET sdk throws HttpException
                // for some reason so to be compatible
                // we have to do it to :(
                throw new HttpException(ex.Message, ex);
            } catch (IOException ex) {
                throw new HttpException(ex.Message, ex);
            } catch (FormatException ex) {
                throw new HttpException(ex.Message, ex);
            } catch (SocketException ex) {
                throw new HttpException(ex.Message, ex);
            }
        }
        // sends a single part mail to the server
        void SendSinglepartMail(MailMessageWrapper msg)
        {
            // write the header
            smtp.WriteHeader(msg.Header);

            // send the mail body
            smtp.WriteBytes(msg.BodyEncoding.GetBytes(msg.Body));
        }
        public void Send(MailMessageWrapper msg)
        {
            if (msg.From == null)
            {
                throw new SmtpException("From property must be set.");
            }

            if (msg.To == null)
            {
                if (msg.To.Count < 1)
                {
                    throw new SmtpException("Atleast one recipient must be set.");
                }
            }

            StartSend(msg);
            // start with a reset incase old data
            // is present at the server in this session
            smtp.WriteRset();

            // write the mail from command
            smtp.WriteMailFrom(msg.From.Address);

            // write the rcpt to command for the To addresses
            foreach (MailAddress addr in msg.To)
            {
                smtp.WriteRcptTo(addr.Address);
            }

            // write the rcpt to command for the Cc addresses
            foreach (MailAddress addr in msg.Cc)
            {
                smtp.WriteRcptTo(addr.Address);
            }

            // write the rcpt to command for the Bcc addresses
            foreach (MailAddress addr in msg.Bcc)
            {
                smtp.WriteRcptTo(addr.Address);
            }

            // write the data command and then
            // send the email
            smtp.WriteData();

            if (msg.Attachments.Count == 0)
            {
                SendSinglepartMail(msg);
            }
            else
            {
                SendMultipartMail(msg);
            }

            // write the data end tag "."
            smtp.WriteDataEndTag();
        }
	public void Send( MailMessageWrapper msg ) {
	    
	    if( msg.From == null ) {
		throw new SmtpException( "From property must be set." );
	    }

	    if( msg.To == null ) {
		if( msg.To.Count < 1 ) throw new SmtpException( "Atleast one recipient must be set." );
	    }
	    
	    	    
	    // start with a reset incase old data
	    // is present at the server in this session
	    smtp.WriteRset();
	    
	    // write the mail from command
	    smtp.WriteMailFrom( msg.From.Address );
	    
	    // write the rcpt to command for the To addresses
	    foreach( MailAddress addr in msg.To ) {
		smtp.WriteRcptTo( addr.Address );
	    }

	    // write the rcpt to command for the Cc addresses
	    foreach( MailAddress addr in msg.Cc ) {
		smtp.WriteRcptTo( addr.Address );
	    }

	    // write the rcpt to command for the Bcc addresses
	    foreach( MailAddress addr in msg.Bcc ) {
		smtp.WriteRcptTo( addr.Address );
	    }
	    
	    // write the data command and then
	    // send the email
	    smtp.WriteData();
		
	    if( msg.Attachments.Count == 0 ) {
#if NET_2_0
		//The message might be multipart, if RelatedBodyParts are present
		if (msg.RelatedBodyParts.Count != 0)
			SendMultipartMail (msg);
		else
#endif		
			SendSinglepartMail( msg );	    
	    } else {
		
		SendMultipartMail( msg );
	    
	    }

	    // write the data end tag "."
	    smtp.WriteDataEndTag();

	}
        void StartSend(MailMessageWrapper msg)
        {
            ReadFields(msg);
            Connect();

            // read the server greeting
            smtp.ReadResponse();
            smtp.CheckForStatusCode(220);

            if (usessl || (username != null && password != null && authenticate != 1))
            {
                smtp.WriteEhlo(Dns.GetHostName());

                if (usessl)
                {
                    bool isSSL = smtp.WriteStartTLS();
                    if (isSSL)
                    {
                        ChangeToSSLSocket();
                    }
                }

                if (username != null && password != null && authenticate != 1)
                {
                    smtp.WriteAuthLogin();
                    if (smtp.LastResponse.StatusCode == 334)
                    {
                        smtp.WriteLine(Convert.ToBase64String(Encoding.ASCII.GetBytes(username)));
                        smtp.ReadResponse();
                        smtp.CheckForStatusCode(334);
                        smtp.WriteLine(Convert.ToBase64String(Encoding.ASCII.GetBytes(password)));
                        smtp.ReadResponse();
                        smtp.CheckForStatusCode(235);
                    }
                }
            }
            else
            {
                smtp.WriteHelo(Dns.GetHostName());
            }
        }
        void ReadFields(MailMessageWrapper msg)
        {
            string tmp;

            username = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"];
            password = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
            tmp      = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
            if (tmp != null)
            {
                authenticate = short.Parse(tmp);
            }
            tmp = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
            if (tmp != null)
            {
                usessl = bool.Parse(tmp);
            }
            tmp = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
            if (tmp != null)
            {
                port = int.Parse(tmp);
            }
        }
Example #7
0
		public static void Send (MailMessage message) 
		{
		   		   		    
		    try {
			
			// wrap the MailMessage in a MailMessage wrapper for easier
			// access to properties and to add some functionality
			MailMessageWrapper messageWrapper = new MailMessageWrapper( message );
			
			SmtpClient smtp = new SmtpClient (smtpServer);
			
			smtp.Send (messageWrapper);
		       
			smtp.Close ();
		    
		    } catch (SmtpException ex) {
			// LAMESPEC:
			// .NET sdk throws HttpException
			// for some reason so to be compatible
			// we have to do it to :(
			throw new HttpException (ex.Message, ex);
		    
		    } catch (IOException ex) {
			
			throw new HttpException (ex.Message, ex);
			
		    } catch (FormatException ex) {
			
			throw new HttpException (ex.Message, ex);
		    
		    } catch (SocketException ex) {
			
			throw new HttpException (ex.Message, ex);
			
		    }
		    
		}
	// sends a multipart mail to the server
	private void SendMultipartMail( MailMessageWrapper msg ) {
	    	    
	    // generate the boundary between attachments
	    string boundary = MailUtil.GenerateBoundary();
		
	    // set the Content-Type header to multipart/mixed
	    string bodyContentType = msg.Header.ContentType;

#if NET_2_0
		if (msg.RelatedBodyParts.Count != 0)
			msg.Header.ContentType = String.Format( "multipart/related;\r\n   boundary={0}" , boundary );
		else
#endif
	    msg.Header.ContentType = 
		String.Format( "multipart/mixed;\r\n   boundary={0}" , boundary );
		
	    // write the header
	    smtp.WriteHeader( msg.Header );
		
	    // write the first part text part
	    // before the attachments
	    smtp.WriteBoundary( boundary );
		
	    MailHeader partHeader = new MailHeader();
	    partHeader.ContentType = bodyContentType;		

#if NET_1_1
		// Add all the custom headers to body part as specified in 
		//Fields property of MailMessageWrapper

        //Remove fields specific for authenticating to SMTP server.
        //Need to incorporate AUTH command in SmtpStream to handle  
        //Authorization info. Its a temporary fix for Bug no 68829.
        //Will dig some more on SMTP AUTH command, and then implement
        //Authorization. - Sanjay

        if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] != null)
            msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate");
        if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"] != null)
            msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendusername");
        if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"] != null)
            msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendpassword");
		partHeader.Data.Add (msg.Fields.Data);
#endif

	    smtp.WriteHeader( partHeader );
	  
	    // FIXME: probably need to use QP or Base64 on everything higher
	    // then 8-bit .. like utf-16
	    smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body )  );

	    smtp.WriteBoundary( boundary );

#if NET_2_0
		for (int i = 0; i < msg.RelatedBodyParts.Count; i++) {
			RelatedBodyPart rbp = (RelatedBodyPart) msg.RelatedBodyParts [i];
			FileInfo file = new FileInfo (rbp.Path);
			MailHeader header = new MailHeader ();
			header.ContentLocation = rbp.Path;
			header.ContentType = String.Format (MimeTypes.GetMimeType (file.Name) + "; name=\"{0}\"",file.Name);
			//If content id and ContentLocation both are present
			//in mime header of a mail, than RelatedBodyPart of mail
			//doesnt show up in a machine other than from which mail
			//was sent, and hence only one of them is inserted in 
			//header. Need to check how the things go when another 
			//body part refers the content with the content id specified
			/*if (rbp.Name != null)
				header.Data.Add ("Content-ID", "<"+rbp.Name+">");*/
				
			header.ContentTransferEncoding = "Base64";
			header.ContentDisposition = String.Format( "inline; filename=\"{0}\"" , file.Name );
			
			smtp.WriteHeader (header);
			FileStream rbpStream = new FileStream (file.FullName, FileMode.Open);
			IAttachmentEncoder rbpEncoder = new Base64AttachmentEncoder ();
			rbpEncoder.EncodeStream (rbpStream, smtp.Stream);
			rbpStream.Close();
			smtp.WriteLine( "" );
			
			if (i < (msg.RelatedBodyParts.Count - 1)) {
				smtp.WriteBoundary (boundary);
			} else {
				if (msg.Attachments.Count == 0)
			   		 smtp.WriteFinalBoundary (boundary);
				else
			    		smtp.WriteBoundary (boundary);
					
			}						
		}
#endif	    
	    // now start to write the attachments
	    
	    for( int i=0; i< msg.Attachments.Count ; i++ ) {
		MailAttachment a = (MailAttachment)msg.Attachments[ i ];
			
		FileInfo fileInfo = new FileInfo( a.Filename );

		MailHeader aHeader = new MailHeader();
		
		aHeader.ContentType = 
		    String.Format (MimeTypes.GetMimeType (fileInfo.Name) + "; name=\"{0}\"",fileInfo.Name);
		
		aHeader.ContentDisposition = 
		    String.Format( "attachment; filename=\"{0}\"" , fileInfo.Name );
		
		aHeader.ContentTransferEncoding = a.Encoding.ToString();
		    		
		smtp.WriteHeader( aHeader );
		   
		// perform the actual writing of the file.
		// read from the file stream and write to the tcp stream
		FileStream ins = new FileStream( fileInfo.FullName  , FileMode.Open );
		
		// create an apropriate encoder
		IAttachmentEncoder encoder;
		if( a.Encoding == MailEncoding.UUEncode ) {
		    encoder = new UUAttachmentEncoder( 644 , fileInfo.Name  );
		} else {
		    encoder = new Base64AttachmentEncoder();
		}
		
		encoder.EncodeStream( ins , smtp.Stream );
		
		ins.Close();
		
		    
		smtp.WriteLine( "" );
		
		// if it is the last attachment write
		// the final boundary otherwise write
		// a normal one.
		if( i < (msg.Attachments.Count - 1) ) { 
		    smtp.WriteBoundary( boundary );
		} else {
		    smtp.WriteFinalBoundary( boundary );
		}
		    
	    }
	       
	}
	// sends a single part mail to the server
	private void SendSinglepartMail( MailMessageWrapper msg ) {
	    	    	    
	    // write the header
	    smtp.WriteHeader( msg.Header );
	    
	    // send the mail body
	    smtp.WriteBytes( msg.BodyEncoding.GetBytes( msg.Body ) );

	}
Example #10
0
		void ReadFields (MailMessageWrapper msg)
		{
			string tmp;
			username = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"];
			password = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"]; 
			tmp = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]; 
			if (tmp != null)
				authenticate = short.Parse (tmp);
			tmp = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]; 	
			if (tmp != null)
				usessl = bool.Parse (tmp);
			tmp = msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]; 
			if (tmp != null)
				port = int.Parse (tmp);
		}
Example #11
0
		// sends a multipart mail to the server
		void SendMultipartMail (MailMessageWrapper msg)
		{    
			// generate the boundary between attachments
			string boundary = MailUtil.GenerateBoundary ();
		
			// set the Content-Type header to multipart/mixed
			string bodyContentType = msg.Header.ContentType;

			msg.Header.ContentType = String.Concat ("multipart/mixed;\r\n   boundary=", boundary);
		
			// write the header
			smtp.WriteHeader (msg.Header);
		
			// write the first part text part
			// before the attachments
			smtp.WriteBoundary (boundary);
		
			MailHeader partHeader = new MailHeader ();
			partHeader.ContentType = bodyContentType;		

			// Add all the custom headers to body part as specified in 
			//Fields property of MailMessageWrapper

			//Remove fields specific for authenticating to SMTP server.
			//Need to incorporate AUTH command in SmtpStream to handle  
			//Authorization info. Its a temporary fix for Bug no 68829.
			//Will dig some more on SMTP AUTH command, and then implement
			//Authorization. - Sanjay

			if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] != null)
				msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate");
			if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"] != null)
				msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendusername");
			if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"] != null)
				msg.Fields.Data.Remove ("http://schemas.microsoft.com/cdo/configuration/sendpassword");
			partHeader.Data.Add (msg.Fields.Data);

			smtp.WriteHeader (partHeader);
	  
			// FIXME: probably need to use QP or Base64 on everything higher
			// then 8-bit .. like utf-16
			smtp.WriteBytes (msg.BodyEncoding.GetBytes (msg.Body) );

			smtp.WriteBoundary (boundary);

			// now start to write the attachments
	    
			for (int i=0; i< msg.Attachments.Count ; i++) {
				MailAttachment a = (MailAttachment)msg.Attachments[ i ];
				FileInfo fileInfo = new FileInfo (a.Filename);
				MailHeader aHeader = new MailHeader ();
		
				aHeader.ContentType = 
					String.Concat (MimeTypes.GetMimeType (fileInfo.Name), "; name=\"", fileInfo.Name, "\"");
		
				aHeader.ContentDisposition = String.Concat ("attachment; filename=\"", fileInfo.Name, "\"");
				aHeader.ContentTransferEncoding = a.Encoding.ToString();
				smtp.WriteHeader (aHeader);
		   
				// perform the actual writing of the file.
				// read from the file stream and write to the tcp stream
				FileStream ins = fileInfo.OpenRead ();
		
				// create an apropriate encoder
				IAttachmentEncoder encoder;
				if (a.Encoding == MailEncoding.UUEncode)
					encoder = new UUAttachmentEncoder (644, fileInfo.Name );
				else
					encoder = new Base64AttachmentEncoder ();
		
				encoder.EncodeStream (ins, smtp.Stream);
		
				ins.Close ();
				smtp.WriteLine ("");
		
				// if it is the last attachment write
				// the final boundary otherwise write
				// a normal one.
				if (i < (msg.Attachments.Count - 1))
					smtp.WriteBoundary (boundary);
				else
					smtp.WriteFinalBoundary (boundary);
			}
		}
Example #12
0
		void StartSend (MailMessageWrapper msg)
		{
			ReadFields (msg);
			Connect ();

			// read the server greeting
			smtp.ReadResponse ();
			smtp.CheckForStatusCode (220);

			if (usessl || (username != null && password != null && authenticate != 1)) 
			{
				smtp.WriteEhlo (Dns.GetHostName ());

				if (usessl) {
					bool isSSL = smtp.WriteStartTLS ();
					if (isSSL)
						ChangeToSSLSocket ();
				}

				if (username != null && password != null && authenticate != 1) {
					smtp.WriteAuthLogin ();
					if (smtp.LastResponse.StatusCode == 334) {
						smtp.WriteLine (Convert.ToBase64String (Encoding.ASCII.GetBytes (username)));
						smtp.ReadResponse ();
						smtp.CheckForStatusCode (334);
						smtp.WriteLine (Convert.ToBase64String (Encoding.ASCII.GetBytes (password)));
						smtp.ReadResponse ();
						smtp.CheckForStatusCode (235);
					}
				}
			} else  {
				smtp.WriteHelo (Dns.GetHostName ());
			}
		}
        // sends a multipart mail to the server
        void SendMultipartMail(MailMessageWrapper msg)
        {
            // generate the boundary between attachments
            string boundary = MailUtil.GenerateBoundary();

            // set the Content-Type header to multipart/mixed
            string bodyContentType = msg.Header.ContentType;

            msg.Header.ContentType = String.Concat("multipart/mixed;\r\n   boundary=", boundary);

            // write the header
            smtp.WriteHeader(msg.Header);

            // write the first part text part
            // before the attachments
            smtp.WriteBoundary(boundary);

            MailHeader partHeader = new MailHeader();

            partHeader.ContentType = bodyContentType;

#if NET_1_1
            // Add all the custom headers to body part as specified in
            //Fields property of MailMessageWrapper

            //Remove fields specific for authenticating to SMTP server.
            //Need to incorporate AUTH command in SmtpStream to handle
            //Authorization info. Its a temporary fix for Bug no 68829.
            //Will dig some more on SMTP AUTH command, and then implement
            //Authorization. - Sanjay

            if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] != null)
            {
                msg.Fields.Data.Remove("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate");
            }
            if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendusername"] != null)
            {
                msg.Fields.Data.Remove("http://schemas.microsoft.com/cdo/configuration/sendusername");
            }
            if (msg.Fields.Data ["http://schemas.microsoft.com/cdo/configuration/sendpassword"] != null)
            {
                msg.Fields.Data.Remove("http://schemas.microsoft.com/cdo/configuration/sendpassword");
            }
            partHeader.Data.Add(msg.Fields.Data);
#endif

            smtp.WriteHeader(partHeader);

            // FIXME: probably need to use QP or Base64 on everything higher
            // then 8-bit .. like utf-16
            smtp.WriteBytes(msg.BodyEncoding.GetBytes(msg.Body));

            smtp.WriteBoundary(boundary);

            // now start to write the attachments

            for (int i = 0; i < msg.Attachments.Count; i++)
            {
                MailAttachment a        = (MailAttachment)msg.Attachments[i];
                FileInfo       fileInfo = new FileInfo(a.Filename);
                MailHeader     aHeader  = new MailHeader();

                aHeader.ContentType =
                    String.Concat(MimeTypes.GetMimeType(fileInfo.Name), "; name=\"", fileInfo.Name, "\"");

                aHeader.ContentDisposition      = String.Concat("attachment; filename=\"", fileInfo.Name, "\"");
                aHeader.ContentTransferEncoding = a.Encoding.ToString();
                smtp.WriteHeader(aHeader);

                // perform the actual writing of the file.
                // read from the file stream and write to the tcp stream
                FileStream ins = fileInfo.OpenRead();

                // create an apropriate encoder
                IAttachmentEncoder encoder;
                if (a.Encoding == MailEncoding.UUEncode)
                {
                    encoder = new UUAttachmentEncoder(644, fileInfo.Name);
                }
                else
                {
                    encoder = new Base64AttachmentEncoder();
                }

                encoder.EncodeStream(ins, smtp.Stream);

                ins.Close();
                smtp.WriteLine("");

                // if it is the last attachment write
                // the final boundary otherwise write
                // a normal one.
                if (i < (msg.Attachments.Count - 1))
                {
                    smtp.WriteBoundary(boundary);
                }
                else
                {
                    smtp.WriteFinalBoundary(boundary);
                }
            }
        }