I'm have a SMTP client to sent mail throught gmail :
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.