[Solved] Email not receiving with attachments using C# in ASP.Net

Bhavesh23
 
on Aug 18, 2021 04:18 AM
818 Views

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);
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 18, 2021 07:41 AM

You are not calling the SmtpClient Send method.

Refer below article for sending email.

How to send email with attachment in ASP.Net