I have a aspx page. I want to send mail along with attachment. But i dont want to hard code all the details. I want server to take value from the textbox and then send mail. Also i want the form to open the file open dialog box & search for the file and then send the selected file as attachment. I know how to do that in windows application. But am not able to do it web application. Please guide me how to it. Currently i have coded all the details like sender mail id, sender mail password, reciever mail id, subject, message, file path. I also have to type in the same thing when i run the application. I dont want to type twice. I want to type only when i run the application. Please guide me in doing that way.
Thanks in advance,
Avinash
Loading
Vijay YadavPosted Mar 15, 2011, 3:28 AM
You have to use this namspace i.e. using System.Net.Mail;
on button click write this code: string from = txtFrom.Text; //Replace this with your from whom you want to send mail
string to = txtTo.Text; //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from, "Any Name", System.Text.Encoding.UTF8);
mail.Bcc.Add(txtBcc.Text);
mail.CC.Add(txtCc.Text);
mail.Subject = txtSubject.Text;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = txtMessage.Text;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(username, password);// username and password of gmail
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
if (!txtAttach.Text.Equals(""))
{
Attachment objAttach = new Attachment(txtAttach.Text);
if (objAttach != null)
{
mail.Attachments.Add(objAttach);
}
}
client.Send(mail);
MessageBox.Show("Mail sent successfully.");
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
MessageBox.Show(errorMessage);
}
and you have to drag and drop Openfile dialog for attaching file and on button so that on click of that button dialog box should get opened.
write the below code on attach button
DialogResult dgAttach = openAttachFileDialog.ShowDialog();
if (dgAttach.Equals(DialogResult.OK))
{
txtAttach.Text = openAttachFileDialog.FileName;
}
Suthish NairPosted Mar 16, 2011, 3:33 PM