How would I tell my application to send an email to a subscriber? How would I send it every week starting from the time the appication is launched.
Use the classes in the System.Net.Mail namespace to send emails from your app. Specifically, the MailMessage class serves this purpose.
To send emails periodically, you can use a timer (use System.Timers.Timer, for example) or you could use the built-in Windows task scheduler, which has rich functionality and runs as a service, so that you don't need to keep an interactive session open on your machine. I can give you a more detailed answer if you provide more details about the type of app you're developing.
I use this method to use gmail, and it works well for me.
var fromAddress = new MailAddress("From");
var toAddress = new MailAddress("To");
string fromPassword = textBox4.Text;
const string subject = "Test";
const string body = "Test Finished";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
Attachment attachf = new Attachment("C:\\file.txt");
message.Attachments.Add(attachf);
smtp.Send(message);
}
}
I haven't created anything directly like this myself but I've seen solutions that use a Scheduled Task on the Server which are set to run on a certain date/time a small script that carries out what is required. This assumes you have your own server though...
Related
I made a random Gmail account.I want that account to send to my personal Gmail account something, using C#. I found out about FluentEmail these days. This is the class:
public static class EmailSender
{
//the random account mail and password
private static string username = "blabla";
private static string password = "blabla";
static EmailSender()
{
NetworkCredential myCredentials = new NetworkCredential();
myCredentials.UserName = username;
myCredentials.Password = password;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = myCredentials,
Port = 465,
EnableSsl = true,
};
var sender = new SmtpSender(() => smtp);
Email.DefaultSender = sender;
}
public static async Task SendEmail(string body)
{
var email = await Email
.From(username)
.To("mymail")
.Subject("NEW BUG")
.Body(body)
.SendAsync();
if (email.Successful)
{
Acr.UserDialogs.UserDialogs.Instance.Alert("Your message was sent!", "Succesful", "Ok");
}
}
}
I don't know why but nothing happens when I click the send button.When I click it 2 times in a row the app crashes. I put a breakpoint at the start of the SendEmail function but I still don't know what's wrong. Maybe I set something wrong in the constructor?Thanks.
I'm not familiar with FluentEmail, but there are two points which are obviously problematic:
You check whether the mail has been sent successfully (if (email.Successful)). If it didn't, you just... do nothing. Instead, find out why the mail has not been sent and display that information instead. If your return object has a property Successful, I'm pretty sure it also has a property telling you what went wrong.
I did glimpse at the FluentEmail source code (based on your question), and it apparently uses .NET's built-in SmtpClient class. SmtpClient does not support SMTPS at port 465. The supported options are unauthenticated SMTP at port 25 or STARTTLS at port 587.
I have attempted to send an email using the GMail SMTP, and followed the guides on various other questions but I still cannot get emails to send from my GMail account.
This is the code I'm using:
protected void emailSend_Click(object sender, EventArgs e)
{
var fromAddress = new MailAddress(inputEmail.Text, inputName.Text);
var toAddress = new MailAddress("spikey666#live.co.uk", "Liane Stevenson");
const string fromPassword = "*********";
const string subject = "Web Dev Wolf Message";
var body = inputMessage.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("webdevelopwolf#gmail.com", fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
These are the things I've checked so far:
Turning on less secure apps on GMail
Checked the Gmail Username and Password are correct
Debugged and checked that all text fields have values and are loaded into variables
Check other port numbers suggested by Gmail help
Turned on POP/IMAP functionality on Gmail
Is there anything else I could be missing?
Before calling SmtpClient.Send(), add:
smtp.UseDefaultCredentials = false;
According to the MSDN SmtpClient page, UseDefaultCredentials is set to false by default, but there seems to be a bug somewhere that is setting it to true. Explicitly set it to false before sending the message and it should be all set.
I am trying to send email like this
var fromAddress = new MailAddress("fromaddress", "From Name");
var toAddress = new MailAddress("toaddress", "To Name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Console.WriteLine("Sent");
Console.ReadLine();
but it gives this error .
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required.
I am sing this code in simple console application on my local host . So whats the issue in my code ?
Update
I changed fromAddress email and it send email successfully . But i don't receive any email in my toAddress email's inbox/Spam .
Try to add DeliveryMethod = SmtpDeliveryMethod.Network when creating SmtpClient.
See post:
https://stackoverflow.com/a/489594/1432770
There is a variety of reasons for this discussed here:
Sending email through Gmail SMTP server with C#
Your code in the first link has worked for me.
Do you use two steps verification?
You need to sign in using application-specific passwords: https://support.google.com/accounts/answer/185833?hl=en
Your code worked for me too!
REMAKING QUESTION
This code doesn't work
The error is: The remote certificate is invalid according to the validation procedure.
var client = new SmtpClient("smtp.domain.com.br", 25000)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true
};
client.Send("emailfrom#domain.com.br", "emailto#gmail.com", "test", "testbody");
// I also tested like this
// client.Send("emailfrom#domain.com.br", "emailto#domain.com", "test", "testbody");
But this code works
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true
};
client.Send("emailfrom#domain.com.br", "emailto#gmail.com", "test", "testbody");
I tested the first code data in the Outlook, and works if I try to send to an email from the same domain.
I believe that the error is some SMTP configuration, but I don't know how to solve this. Any help?
which smtp server you using ?
if you are using gmail smtp server then you must use port number 587 that is required to send through gmail
based on the answer below you can pass in the values from the .config file but you will have to change what I have ..test this using hard coded values first if you like then convert the working version to use param values
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var _smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials =
new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
_smtpClient.Send(message);
}
did you try port 465. may be you can check this article.
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
I solve this using the digital certificate generated in the outlook and putting it on the server, where my application is running.
With the code in my question plus the certificate I finally made this works.
Here is a link on How to do this: create your own digital certificate
My app is sending 3 emails at the same time to the recipient, one being the correct email, and the other two contain the subject line, but an empty message. Could this code some how cause that? If not what do you suggest?
var fromAddress = new MailAddress(domainAddress, displayName);
var toAddress = new MailAddress(oInfo.SiteUser.email, oInfo.customerName);
var Bcc = new MailAddress("deleted");
var smtp = new SmtpClient
{
Host = SmtpHost(),
Port = SmtpPort(),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(SmtpUsername(), SmtpPassword())
};
using (var msg = new MailMessage(fromAddress, toAddress)
{
IsBodyHtml = true,
Subject = "Confirmation for your recent order at " + displayName,
Body = body
})
{
msg.Bcc.Add(Bcc);
smtp.Send(msg);
}
No, that code won't send more than one mail.
Either you have some more code that is sending mail, or you are executing that code three times, but with different values for body.
The only problem I can see with that code is the line
var Bcc = new MailAddress("deleted");
but I assume you modified it for posting here?
I can't see an issue that would cause what you're seeing. I'd check the headers in the emails for clues. Also capturing network traffic on the machine sending the emails could help.