How to send email from my Asp.net c# website from my domain email id with attachment.
//Solution
// Email
using System.Net;
using System.Net.Mail;
public void Gomail()
{
try
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add(txtemail.Value);
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("nktadmin@nktechno.net.net");
msg.From = address;
msg.Subject = "N K Technology";
string msgbody = "Dear " + "User" + " ,<table align=center width=800><tr><td><B> Your Details are as below </B> </td> </tr><tr><td>Name </td><td> :</td><td> <b>" + txtname.Value + "</b> </tr><tr><td>Designation </td><td> :</td><td> <b>" + txtdesign.Value + "</b> </tr><tr><td>Contact Number </td><td>: </td><td><b>" + txtcontct.Value + "</b></td></tr><tr><td>Address </td><td>: </td><td><b>" + txtadd.Value + "</b></td></tr><tr><td>Email </td><td>: </td><td><b>" + txtemail.Value + " </b></td> </tr><tr><td>Message </td><td> :</td><td> <b>" + "Demo" + "</b></td></tr><tr><td colspan=7 align=right><font size=1><span style='font-size:8.5pt;font-family:TimesNewRoman'><br>Sincerely, <br><br>N.K. Technology<br></span></font><br><font style='font-size:9px'>This mail is autogenerated to confirm your Enquiry</font> </tr></tr></table>";
msg.Body = msgbody.ToString();
SmtpClient client = new SmtpClient();
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
//lblResult.Text = "Your message was sent!";
}
catch (Exception ex)
{
//If the message failed at some point, let the user know
//lblResult.Text = ex.ToString(); //alt text "Your message failed to send, please try again."
}
}
check this code, is this correct, if not please rewrite this code and reply urgently. Deadline till 5 pm today.
Thanks in advance
please check this article for sending email using asp.net C#, it has the complete working code, with attachments also
Check section 3 for sending Email in ASP.NET MVC using C# (It will work for Web-forms ASP.NET also, as C# code required is always same in MVC and Web-forms)
I tried executing your code locally on my pc, and got an error Unable to connect to the remote server while trying to send mail and
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 72.167.234.197:25
I have found a similar query here https://stackoverflow.com/questions/5211858/smtpclient-send-exception
The issue is related to the server there is no issue in the code if your server doesn't require credentials and port is 25, so try to send mail asynchronously and check firewall also.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly