Unable to upload attachment in send mail demo
Guys I am stuck with a problem and want your genuine suggestion..
Actually the problem is that i am trying to send a mail using c#.net..
There is no issues while sending mail but when i try to include an attachment with it is not taking the path of the concerned directory.. But when i am attaching the file from the root directory here i have my codes it is working absolutely well..
Plz look at the code below and give some suggestion to rectify the problem..
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(txtTo.Text);
mail.From = new MailAddress("orangesoftech2012@gmail.com");
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(FileUpload1.FileName);
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("orangesoftech2012@gmail.com", "orangeorange");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
Response.Write("<script language='javascript'>alert('MAIL SENT');</script>");
Response.Write("<script language='javascript'>alert('Thank You for using VishalMail');</script>");
}
catch (Exception ex)
{
labelError.Text = ex.Message;
}
}
geniusvishal
Junior Poster in Training
58 posts since Jan 2012
Reputation Points: 9
Solved Threads: 4
Needless to say i am using demo gmail id over here so better try with your own id while debugging changing the webconfig as well...
geniusvishal
Junior Poster in Training
58 posts since Jan 2012
Reputation Points: 9
Solved Threads: 4
Guys i solved this problem.. The code is:
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(txtTo.Text);
mail.From = new MailAddress("orangesoftech2012@gmail.com");
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream,FileUpload1.FileName);
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("orangesoftech2012@gmail.com", "********");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
Response.Write("<script language='javascript'>alert('MAIL SENT');</script>");
Response.Write("<script language='javascript'>alert('Thank You for using VishalMail');</script>");
}
catch (Exception ex)
{
labelError.Text = ex.Message;
}
}
geniusvishal
Junior Poster in Training
58 posts since Jan 2012
Reputation Points: 9
Solved Threads: 4
hii..
Even i did the same but then it didnt work for me ... it was working good in my previous project :-( ...couldnt find where i have gone wrong now...
kamilacbe
Junior Poster in Training
77 posts since Jun 2010
Reputation Points: 10
Solved Threads: 10
Then post the code for your project... Lets see what is the problem....
geniusvishal
Junior Poster in Training
58 posts since Jan 2012
Reputation Points: 9
Solved Threads: 4
hi..this is my code ....did the same
try
{
MailMessage mail = new MailMessage();
mail.To.Add(txtto.text);
mail.From = new MailAddress("username");
mail.Subject = "well";
mail.Body = "hii";
// mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("username", "password");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
kamilacbe
Junior Poster in Training
77 posts since Jun 2010
Reputation Points: 10
Solved Threads: 10
Have you changed the configuration in web.config??
geniusvishal
Junior Poster in Training
58 posts since Jan 2012
Reputation Points: 9
Solved Threads: 4
1> IF not then configure the settings for system.net... mention the network credentials over dere alongwith the port number....
2> Change the IMAP/POP settings in your userid...
3> If still getting error paste the error msg with error img...
geniusvishal
Junior Poster in Training
58 posts since Jan 2012
Reputation Points: 9
Solved Threads: 4
hii..
I forgot to update my username in webconfig file ...thank you soo much for your prompt reply...got fixed it ..:-)
kamilacbe
Junior Poster in Training
77 posts since Jun 2010
Reputation Points: 10
Solved Threads: 10