How to send mail from Domain server in Asp.net c#?


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


Asked by:- VivekVishwas
0
: 5358 At:- 11/4/2017 9:16:02 AM
Asp.net C# SQl send-email-from-domain-server

check port number, add credentials for smtp address also 0
By : vikas_jk - at :- 11/4/2017 10:20:08 AM






2 Answers
profileImage Answered by:- vikas_jk

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)

1
At:- 11/4/2017 10:18:48 AM Updated at:- 11/4/2017 10:23:21 AM
This code only works on Gmail , I want to send email from domain server email address like help@qawithexperts.com 0
By : VivekVishwas - at :- 11/4/2017 10:26:10 AM
yes, it works for the domain also, we are sending email from info@qawithexperts.com using similar code. If you are getting any error, please attach images of error by editing question, as i have commented, check your port, gmail uses 587, godaddy uses port 465 for SSL and 80, 3535, 25 for non SSL, i have sent the email using above code from godaddy hosted Workspace also, mainly issue is related to port. Check port and SMTP client credentials also in you question code, i don't see smpty client password/email 0
By : vikas_jk - at :- 11/4/2017 10:50:07 AM


profileImage Answered by:- jaya

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.

1
At:- 11/5/2017 12:09:34 PM Updated at:- 11/5/2017 12:09:59 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use