Send Email through Asp.Net Web Api [duplicate] - c#

This question already has answers here:
How to send email in ASP.NET C#
(14 answers)
Closed 7 years ago.
My client want to send E-mail through asp.net Web API. I am new in web services and have 0 knowledge of this please guide me how to achieve this task and if anyone can provide the code, there must be some web service available like this.
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}

The .NET mail API sends emails via SMTP. The ASP.NET Web API allows you to host a REST interface.
You said you know how to program with both, so do that.
Create your Web API method which receives the necessary arguments, exactly like you would for a normal method that will do your emailing duties for you. Then create your route to the method.
If you have issues with either not working, then write tests.
Documentation for Web API: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Documentation for SmtpClient: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx

# Call this function in your WebApi controller #
=========================================================
private void sendEmailViaWebApi()
{
string subject = "Email Subject";
string body = "Email body";
string FromMail = "shahid#reckonbits.com.pk";
string emailTo = "reciever#reckonbits.com.pk";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("mail.reckonbits.com.pk");
mail.From = new MailAddress(FromMail);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("shahid#reckonbits.com.pk", "your password");
SmtpServer.EnableSsl = false;
SmtpServer.Send(mail);
}

Related

How do you schedule an email in C# using Gmail?

So, this is my email function, that sends an email using Gmail's SMTP:
public void Email(string subject, string body)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(email, emailname);
message.To.Add(new MailAddress(targetemail, targetname));
message.Subject = subject;
smtp.EnableSsl = true;
message.IsBodyHtml = true;
message.Attachments.Add(new Attachment(folder + #"\" + cbPrezentacja.SelectedItem.ToString() + "." + extension));
message.Body = body;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(email, password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
It works however, how would I schedule the email for a specific time? I don't mean making a Windows schedule/timer on the host (that requires leaving it on) or whatever, I want to do it via Gmail.
https://i.stack.imgur.com/eNG9D.png < This is how it works using Gmail. (sorry for the weird tint)
Is this even possible using System.Net.Mail or do I need a special Gmail API for that (if so, how?)? This is a private application for 1 computer, so it doesn't need to be super secure.
Thanks!
You can't.
There's nothing in SMTP to schedule it, when you do smtp.Send(message); the message is sent that exact moment.
Gmail has an API, but (as far I can see) it doesn't offer this functionality, so right now the only way to do it would be exactly what you say you don't want: your app should somehow wait till the desired time and send it.

Gmail SMTP failure using c# SmtpClient

Our application has the possibility to send emails via SmtpClient. One of our customer is trying to do so by using his gmail account and it fails resulting in a timeout. However, from our testing lab it works just fine (with another gmail account).
In the trace i can see that the server is not answering with Certificate, Server Key Exchange, Server Hello Done. And im wondering what can be the cause for this?
I also noticed in the traces, the customer is using TLSv1 so I tried to replicate the error on a Windows7 system but still it works for me.
oSmtp = new SmtpClient(this.host, this.port);
oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
oSmtp.EnableSsl = ssl;
NetworkCredential oCredential = new NetworkCredential(this.userName, this.password);
oSmtp.Credentials = oCredential;
oSmtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = null;
oSmtp.SendAsync(oMsg, userState);
As far as the code goes, enableSsl is true, the port is 587 and we also instructed our customer to check his gmail account and allow less secure applications.
We will ask the customer for more specific details and try to put more traces in our application, but i would like to know if there is anything that can prevent the server to answer with Certificate,...
Inspecting the traces revealed no significant difference between customers Client Hello and our test Client Hello.
Thanks!
This is a working sample i used few days ago:
string fromAddress = "fromaddress#gmail.com";
var toAddress = new MailAddress("fake#email.com", "To person");
const string fromPassword = "pass";
const string subject = "test";
const string body = "Hey now!!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
//mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
also make sure to enable Less secure app access for the gmail you are using to send data:

how to send email in Asp.net mvc 5 [duplicate]

This question already has answers here:
Sending email in .NET through Gmail
(26 answers)
Closed 3 years ago.
What code should I use to send email in C#?
I tried to find a specific code so that I could send an email from my website.
And then I get an error:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
public void sendEmail(string toEmail, string subject, string emailBody)
{
string senderEmail = "My_Email";
string senderPassword = "My_Email_Password";
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 500000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(senderEmail, senderPassword);
MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
client.Send(mailMessage);
}
Do I need to use Google API??
Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.
You need to go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps. So that you will be able to login from all apps.

Can clients send email to each other via your website?

I am working on an online shopping website. I want the users directly contact each other via emails by using a web form. I have tried different ways. but it didn't work.
I was also trying to use the gmail smtp as follow:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("example#gmail.com", "mypassword");
client.EnableSsl = true;
string fromAddress = "buyer#gmail.com";
string messageSubject = "Your Subject";
string body = "Content";
var supEmail = "seller#yahoo.com";
MailMessage mm = new MailMessage(fromAddress, supEmail, messageSubject, body);
MailAddress copy = new MailAddress("notifications#mydomain.com");
mm.CC.Add(copy);
//Set up your encoding
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.Priority = System.Net.Mail.MailPriority.High;
//Send your message
client.Send(mm);
Any idea or suggestions?
This won't work. Google won't allow you to send an email from an account you don't own... so if you want to send an email via Google then it needs to be from either the Google email account your are validating with or associated with that account.

Email not working,Not able to receive email for yahoo email id

I am stuck and not able to receive email from yahoo id(if the sender email id is of yahoo).
code is working fine not giving me any error and i am receiving email fromm gmail id.
i am using localhost (not in local machine on live server).
hosting server : smtp.snapgrid.com
also used authentication,enable ssl , using proper port for ssl.
on snapgrid i do check so i got is mail from yahoo is blocked and the message is,
message :
Type: blocked
Reason: 550 5.7.1 Unauthenticated email from yahoo.com is not accepted due to domain's
DMARC policy. Please contact administrator of yahoo.com domain if this was a legitimate
mail. Please visit http://support.google.com/mail/answer/2451690 to learn about DMARC
initiative.
please help...
code i used to send is(its working fine just giving for idea):
Method 1:
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.Host = ConfigurationManager.AppSettings["strSMTPServer"];
string BODY_FORMAT = ConfigurationManager.AppSettings["EmailBodyContentFormat"];
MailMessage objMailMessage = new MailMessage(from.Trim(), to.Trim(), subject.Trim(), body.Trim());
objSMTPClient.UseDefaultCredentials = false;
if (BODY_FORMAT.ToUpper() == "HTML")
objMailMessage.IsBodyHtml = true;
else if (BODY_FORMAT.ToUpper() == "TEXT")
{
body = StripTags(body);
objMailMessage.IsBodyHtml = false;
objMailMessage.Body = body.ToString().Trim();
}
else
return false;
objSMTPClient.Send(objMailMessage);
return true;
Method 2:
SmtpClient oMail = new SmtpClient();
MailMessage msg = new MailMessage();
MailAddress Madd = new MailAddress(from, "sunil");
oMail.Host = "smtp.gmail.com";
oMail.Port = 587;
oMail.EnableSsl = true;
oMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
oMail.Credentials = new NetworkCredential("sunil123#mydomain.com", "******");
oMail.Timeout = 20000;
msg.From = Madd;
msg.Body = body.ToString();
msg.To.Add(to);
msg.Subject = subject;
msg.IsBodyHtml = true;
oMail.Send(msg);
return true;
both are working having no bug running without error....
If you are sending via a server belonging to someone like Yahoo, Google or Office365 they expect the sender name of the account to match that that you're sending using in the from address.
For example, this would work on a your local SMTP server:
Message.From = new MailAddress("GrandMasterFlush#domain.com");
However, to get it to send via someone like Yahoo would require you to send it like this:
Message.From = new MailAddress("GrandMasterFlush#domain.com", "Grandmaster Flush");
If the sender name provided does not exactly match that of the account the email will not get sent.

Categories