How to send email using c# web application with pdf or doc file attachment.
user can attach pdf, doc file in mail, file should be validate if user attach other file instead .pdf or .doc then message should be display.
i have make this below code to send attachment in mail with file extension but their is nothing respond after press button.
can you help me to resolve this?
if (resumeUpload.HasFile)
{
var fileName = (resumeUpload.PostedFile.FileName);
try
{
string strExtension = Path.GetExtension(fileName);
if (strExtension != ".docx" || strExtension != ".doc" || strExtension != ".pdf")
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = "Upload only .pdf or .doc file";
}
SmtpClient smtpServer = new SmtpClient();
smtpServer.Host = "smtp.gmail.com";
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "myPassword");
smtpServer.EnableSsl = true;
MailMessage myMsg = new MailMessage();
myMsg.Subject = txtFname.Text;
myMsg.Body = txtMessage.Text;
string toAddress = txtEmail.Text;
myMsg.To.Add(toAddress);
string fromAddress = "Hariti Study Hub <myEmail@gmail.com>";
myMsg.From = new MailAddress(fromAddress);
myMsg.Attachments.Add(new Attachment(resumeUpload.PostedFile.InputStream, fileName));
lblMsg.ForeColor = System.Drawing.Color.GreenYellow;
lblMsg.Text = "Sent Successfully..";
txtFname.Text = "";
txtEmail.Text = "";
txtAddress.Text = "";
txtMessage.Text = "";
txtContactno.Text = "";
txtApplyfor.Text = "";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}