Sending Email Through localhost asp.net - asp.net

public ActionResult Index(EmailModel model)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("sheikh.abm#gmail.com");
message.To.Add(new MailAddress(model.To));
message.Subject = model.Subject;
message.Body = model.Message;
return View();
}
this is my controller action. And in web.config .
<system.net>
<mailSettings>
<smtp from="sheikh.abm#gmail.com">
<network host="\localhost:"/>
</smtp>
</mailSettings>
</system.net>
The problem i got is that the mail didn't send and it didn't show me any error kindly help me.

In order to send the mail you will need to call the send method
e.g
message.Send();

Related

Change from while sending mail using WepAPi

I send a mail using WebApi. Mail sent successfully. I want to change from mail in it and I change it using below code but it take from mail as 'testweb#gmail.com'. I use testweb#gmail.com in webconfig and I want to set from as testfrom#gmail.com.
but not working as per below code and when I got mail it always from 'testweb#gmail.com' instead of 'testfrom#gmail.com'
Note : I use above emails only to ask question while development I use my real mail id.
Is there any other way to achieve this? Or I need to change anything.
Below is my code to send mail:
public static bool SendMail(string toAddress, string subject, string body)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("testfrom#gmail.com");
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
Below is webconfig smpt setting:
<mailSettings>
<smtp from="testweb#gmail.com">
<network host="smtp.gmail.com" port="587" userName="testweb#gmail.com" password="test" />
</smtp>
</mailSettings>
also try with below setting in webconfig:
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="testweb#gmail.com" password="test" />
</smtp>
</mailSettings>
You cant!!
You are using the google SMTP server, it doesn't allows to change the from address, if you want to change the from address try using some other smtp provider that allows so.

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required Error

This code gives me error again and again. I tried many things but i am unable to do please help me.
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("hello#gmail.com");
mail.To.Add("hello#gmail.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("hello#gmail.com", "hello");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
WEB.CONFIG
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="unknownuser6601#gmail.com">
<network host="smtp.gmail.com" port="587" userName="unknownuser6601#gmail.com" password="ASADsohaib" />
</smtp>
</mailSettings>
</system.net>

Email not working from Godaddy Server

First of all, I hope to be at the right place, I apologize in advance if this is not the case, please re-orient myself.
So here I have a worry, I have a website www.artipik.com with two forms, online quote and contact form.
My website is hosted by godaddy and servers were updated summers there for 15 days and my form are no longer work, which put me in a very problematic position. I'm a graphic designer not a coder, hence my difficulty changed the code.
I had the godaddy technical service and he gave it to me I think part of the solution. I found the place to change the code, but I can not seem to fit, I'm testing, I no further error messages but I do not receive any email I send.
My current ASP code is as follows:
<%
'-----version CDONTS-----
Set Mail = Server.CreateObject("CDONTS.NewMail")
Mail.BodyFormat = 1 '0:html/1:plain text
Mail.MailFormat = 1 '0:MIME/1:plain text
Mail.From = envoyeur
Mail.To = receveur
if copie<>vbnullstring then Mail.Bcc = copie
if sujet=vbnullstring then sujet="Formulaire de contact Artipik.com"
Mail.Subject = sujet
Mail.Body = message
Mail.Send
set Mail=Nothing
'response.cookies("email")=envoyeur
%>
and the new code that I have to adapt is the following (Using CDOSYS to Send Email from Your Windows Hosting Account):
// language -- C#
// import namespace
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Thank you for your help.
Julian
System.Web.Mail.MailMessage is obsolete. You should use System.Net.Mail.MailMessage instead. It works almost exactly the same (warning! untested):
private void SendEmail()
{
using (var message = new MailMessage())
{
message.From = new MailAddress("emailaddress#domainname");
var toAddress = new MailAddress("emailaddress#domainname");
message.To.Add(toAddress);
message.Subject = "Test email subject";
message.Body = "Sent at: " + DateTime.Now;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
}
Add this to your web.config:
<configuration>
<system.net>
<mailSettings>
<smtp from="emailaddress#domainname">
<network host="your-smtp-host-address" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Recently I've had the same problem, could solve it throug the next way:
in web.config:
<system.net>
<mailSettings>
<smtp from="contacto#mydomain.com">
<network host="relay-hosting.secureserver.net" password="MyPass" port="25" enableSsl="false" defaultCredentials="false" userName="contacto#mydomain.com"/>
</smtp>
</mailSettings>
</system.net>
in codebehind:
MailMessage m = new MailMessage();
m.IsBodyHtml = true;
m.Body = "My Message";
m.To.Add("jhon.doe#yahoo.com");
m.Subject = "Go Daddy Emails Go Live!";
SmtpClient smtp = new SmtpClient();
smtp.Send(m);

SmtpClient not getting credentials from web.config

On an Asp.Net MVC application I have the following on Web.Config:
<smtp from="noreply#mydomain.com" deliveryMethod="Network">
<network host="smtp.mandrillapp.com" port="25" defaultCredentials="false" userName="gateway#mydomain.com" password="lskdlkdsV6vBfXsQg"/>
</smtp>
I created a SmtpClient on C# as follows:
SmtpClient client = new SmtpClient { UseDefaultCredentials = true };
The Password and Username in client are empty but the host is filled correctly.
Then I tried the following:
SmtpClient client = new SmtpClient { UseDefaultCredentials = false };
Now the Credentials in client is null.
Does anyone has any idea what might be wrong?

how to send a confirmation email in asp.net, cannot get IIS pickup directory error

I want to send a confirmation email to user in registration page. The following code is the related part:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sc.Send(mail);
MESSAGE(true, "Sending mail is successful");
}
catch (Exception)
{
MESSAGE(true, "Sending mail is unsuccessful!");
}
But it does not send an email to related user. I have looked at forums and I added to web.config the following part:
<system.net>
<mailSettings>
<smtp from="myaddress#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="myaddress#gmail.com" password="password" />
</smtp>
</mailSettings>
</system.net>
But anything didn't change. Then i have debugged and it enters into the try statement and when it comes to sc.Send(mail);, it drops to catch. Where is my mistake?
Additionally, during debug i realized that it shows this error: cannot get IIS pickup directory. I have controlled whether I have a smtp service or not from services but i couldn' t see this service. Is this error related to that service?
Thanks in advance.
Why not use GMAIL as the SMTP server, since you are defining a GMAIL account, by defining the host as "smtp.gmail.com", and port at 587?
Specific details here: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
Thanks for your helps.
I have solved this problem. I have changed my code as the following one:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.EnableSsl = true;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "</body></html>";
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Send(mail);
MESAJ(true, "Sending mail is successful");
}
catch (Exception)
{
MESAJ(true, "Sending mail is unsuccessful!");
}
and then i have set my web.config like the following :
<mailSettings>
<smtp deliveryMethod="Network"
from="admin#gmail.com">
<network defaultCredentials="false" host="smtp.gmail.com" port="587"
userName="admin#gmail.com" password="password" />
</smtp>
</mailSettings>
that works.. :)

Resources