Click here to Skip to main content
16,019,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm have a SMTP client to sent mail throught gmail :
C#
public static string SendMessageWithAttachment(string sendTo, string sendFrom, string sendSubject, string sendMessage, ArrayList attachments,string Username,string Password)
        {
            try 
            {
                bool bTest = ValidateEmailAddress(sendTo);
                if (bTest == false)
                {
                    return "Invalid recipient email address: " + sendTo;
                }
                MailMessage message = new MailMessage(sendFrom, sendTo, sendSubject, sendMessage);
                message.From = new MailAddress(sendFrom);
                foreach (string attach in attachments)
                {
                Attachment attached=new Attachment(attach,MediaTypeNames.Application.Octet);
                    message.Attachments.Add(attached);
                }
                SmtpClient client=new SmtpClient();
                if (bTest == true)
                {
                    message.To.Add(new MailAddress(sendTo));
                    message.Subject = sendSubject;
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    client.UseDefaultCredentials = false;
                    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("", "");
                    NetworkCred.UserName = Username;
                    NetworkCred.Password = Password;
                    client.Host = "smtp.gmail.com";
                    client.Port = 587;
                    client.EnableSsl = true;
                    client.Send(message);
                    return "Message sent to " + sendTo + " at " + DateTime.Now.ToString() + ",";
                }
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                
            }
        }
        public static bool ValidateEmailAddress(string emailAddress)
        {
            try
            {
                string TextToValidate = emailAddress;
                Regex expression = new Regex("(?<user>[^@]+)@(?<host>.+)");
                if (expression.IsMatch(TextToValidate))
                {
                    return true;
                }
                else
                {
                    return false;
                }
             }
             catch(Exception)
                {
                    throw;
                }
            
        } 


private void btnSend_Click(object sender, EventArgs e)
       {
           this.Cursor = Cursors.WaitCursor;
Regex emailregex = new Regex("("?<user>[^@]+)@(?<host>.+)");
           Match m = emailregex.Match(tbTo.Text);
           string User = m.Groups["user"].Value;
           string[] arr = txtAttachment.Text.Split(";");
           alAttachments = new ArrayList();
           for(int i=0;i<arr.Length;i++)
           {
               if((String.IsNullOrEmpty(arr[i].ToString().Trim())!=null)||(String.IsNullOrEmpty(arr[i].ToString().Trim())!=String.Empty))
               {
                   alAttachments.Add(arr[i].ToString().Trim());
               }
           }
string result=EmailHandler.Emailer.SendMessage(tbTo.Text,tbxFrom.Text,tbSubject.Text,MessageBody.Text,User,);
                MessageBox.Show(result,"Email Transmittal";);
                this.Cursor = Cursors.Default;
        }
</host></user>

Now, I want my attachment is MIME, so I cant sent image or something like that. How can I modify this code to do that?
NOW I WANT MY ATTACHMENT IS MIME SO I CANT SENT IMAGE OR SOMETHINGS LIKE THAT. HOW CAN I MODIFY THIS CODE TO DO THAT?

Thanks.
Posted
Updated 8-May-11 19:21pm
v2
Comments
RollUpBob 16-Oct-17 9:01am    
This short article demonstrates sending an email with an attachment in c# by using this c# smtp client class.

Just have a look at this tip and you should be able to go ahead: Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
This is not a answer to your problem. But the function ValidateEmailAddress will fail for invalid e-mail addresses check.

Invalid e-mail address - "her@there@my.site.org" - ValidateEmailAddress returns true. Where false is correct.

Check this web site out: http://www.regexlib.com/Search.aspx?k=&c=1&m=-1&ps=20[^]

And the cool App - Expresso[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900