I have a list of data from a database and I have Email Id's in database, i have to send email to those ID's where if its retention date is tomorrow means i have to send an intimation email as reminder by today,i want to send email, and i will use service to send it daily, but my email part is not working..below is my code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace SendingEmail
{
public class SendingMail
{
public static void SendMail(string recipient, string subject, string
body, string attachmentFilename)
{
//method to send email
MailMessage mail = new MailMessage();
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "my password");
string From = "my email#gmail.com";
string To = "email.com";
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
MailMessage msg = new MailMessage(From, To);
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment
(#"C:\Users\rahul.chakrabarty\Desktop\logg.txt");
mail.Attachments.Add(attachment);
SmtpServer.Send(mail);
}
//To cathch Exception
catch (Exception ex)
{
Console.WriteLine("unable to send" + ex);
}
}
}
}
My error is "unable to sendsystem.InvalidOperationException: A from address must be specified"..This is my error
You said
String from = "my email#gmail...
But nowhere have you actually assigned this string to the mail.From property - you've gone and made a new MailMessage using that from address, and called it msg:
MailMessage msg = new MailMessage(From, To)
but you aren't sending the msg mail, you're sending the mail mail:
SmtpServer.Send(mail);
The same problem exists with the To address on the mail variable
Basically, the code is all messed up: you make TWO MailMessage objects, set half the necessary things on one, the other half of necessary things on the other, and then try to send one of them with incomplete details. It's kinda like you copied and pasted two different tutorials together but didn't get enough of either of them to get a complete MailMessage ready for sending
I could have fixed this all up for you and posted working code, but I haven't for two reasons: 1) it's very easy to do these small changes yourself, and 2) I want YOU to do it as a learning exercise rather than just giving you the answer :)
Make sure you don't put a space in the email address either
Related
I am trying to send an email from email1 to email2. But I am getting the following error:
Transaction failed. The server response was: Message rejected: Email address is not verified. The following identities failed the check in region CA-CENTRAL-1: alex.smail#yahoo.ca, Sender Name email1#yahoo.ca, email2#yahoo.com
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
Email("DO TEST");
}
public static void Email(string htmlString)
{
String FROM = "email1#yahoo.ca";
String FROMNAME = "Sender Name";
String TO = "email2#yahoo.com";
String SMTP_USERNAME = "kiffretM5C2PFESI5W";
String SMTP_PASSWORD = "BLeMDSkjioourdvhvbhvhTMHAVfuG6mcAXibbTmQpe7WX";
String HOST = "email-smtp.ca-central-1.amazonaws.com";
int PORT = 587;
String SUBJECT =
"Amazon SES test (SMTP interface accessed using C#)";
// The body of the email
String BODY =
"<h1>Amazon SES Test</h1>" +
"<p>This email was sent through the " +
"<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
"using the .NET System.Net.Mail library.</p>";
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(FROM, FROMNAME);
message.To.Add(new MailAddress(TO));
message.Subject = SUBJECT;
message.Body = BODY;
using (var client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
client.Credentials =
new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
client.EnableSsl = true;
try
{
Console.WriteLine("Attempting to send email...");
client.Send(message);
Console.WriteLine("Email sent!");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}
}
}
}
It would appear that you are using Amazon SES in sandbox mode.
From Moving out of the Amazon SES sandbox - Amazon Simple Email Service:
To help prevent fraud and abuse, and to help protect your reputation as a sender, we apply certain restrictions to new Amazon SES accounts.
We place all new accounts in the Amazon SES sandbox. While your account is in the sandbox, you can use all of the features of Amazon SES. However, when your account is in the sandbox, we apply the following restrictions to your account:
You can only send mail to verified email addresses and domains, or to the Amazon SES mailbox simulator.
You can send a maximum of 200 messages per 24-hour period.
You can send a maximum of 1 message per second.
While operating in sandbox mode, you will need to verify every email address before it can receive email.
I need to be able to send automatic emails to any users who have registered new accounts, changed passwords, and/or created new orders.
I've been given the SendEmail file, which belongs in the "Utilities" folder in my solution.
using System;
using System.Net.Mail;
using System.Net;
namespace SendEmail
{
public static class EmailMessaging
{
public static void SendEmail(String toEmailAddress, String emailSubject, String emailBody)
{
//Create a variable for YOUR TEAM'S Email address
//This is the address that will be SENDING the emails (the FROM address)
String strFromEmailAddress = "email#gmail.com";
//This is the password for YOUR TEAM'S "fake" Gmail account
String strPassword = "Password";
//This is the name of the business from which you are sending
//TODO: Change this to the name of the company you are creating the website for
String strCompanyName = "Team Final Project";
//Create an email client to send the emails
//port 587 is required to work, do not change it
var client = new SmtpClient("smtp.gmail.com", 587)
{
UseDefaultCredentials = false,
//This is the SENDING email address and password
//This will be your team's email address and password
Credentials = new NetworkCredential(strFromEmailAddress, strPassword),
EnableSsl = true
};
//Add anything that you need to the body of the message
//emailBody is passed into the method as a parameter
// /n is a new line – this will add some white space after the main body of the message
//TODO: Change or remove the disclaimer below
String finalMessage = emailBody + "\n\n Thank you, come back again soon!";
//Create an email address object for the sender address
MailAddress senderEmail = new MailAddress(strFromEmailAddress, strCompanyName);
//Create a new mail message
MailMessage mm = new MailMessage();
//Set the subject line of the message (including your team number)
mm.Subject = "Team ## - " + "Thank you!";
//Set the sender address
mm.Sender = senderEmail;
//Set the from address
mm.From = senderEmail;
//Add the recipient (passed in as a parameter) to the list of people receiving the email
mm.To.Add(new MailAddress(toEmailAddress));
//Add the message (passed)
mm.Body = finalMessage;
//send the message!
client.Send(mm);
}
}
}
My problem is that neither I nor my team members know how to implement call this from the controller in a way that will be sent automatically and with the user's email and name. We imagine they will be in the Account and Orders controllers. The accounts controller has the register and change password methods, which work, and the orders controller has the complete order method.
Also, we are not using a confirmation view, it has to be an automatic email.
We need some direction in figuring out where exactly we need to call the method from and how.
The most helpful thing I've found on the internet today is this block of code for a test message that is not intended to be sending automatic emails.
public static void CreateTestMessage(string server)
{
MailAddress from = new MailAddress("sender#gmail.com", "Team Project");
MailAddress to = new MailAddress("reciever#gmail.com", "Customer");
MailMessage message = new MailMessage(from, to);
message.Subject = "Password Changed";
message.Body = #"This is a confirmation email that your password has been changed.";
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
Everything is being coded on MS VS
First create a service for EmailService and put SendEmailAsync Method in it
and call this method on your Controller.
In Method body:
1-Create your message:
var mm = new MailMessage()
2-Then you should build your smtp:
using var smtpClient = new SmtpClient();
3-Then connect it to your server
await smtpClient.ConnectAsync(
"your host",
"port",
SecureSocketOptions.SslOnConnect);
4-Now send your Email:
await smtpClient.SendAsync(mm);
5-Make sure that disconnect from your client:
await smtpClient.DisconnectAsync(true);
Note: It may give you an Exception while Connecting or Sending your Email so don't forget try catch block.
6-For Automation you can use a EmailAccount Table with relation with your Customer Table and keep your message data in it.
for ex: body ,subject..... .
In your Contorller you have your Customer So you can get his EmailAccount from DataBase and pass EmailAccount Entity to SendEmailAsync Method.
instead of creating message in body, get it from EmailAccount Entity and then countinue the Steps.
7-Enjoy it :)
This is my first post on Stack, and I've literally just started programming, so please, be patient.
I'm attempting to send an email to "x" email address when Button 1 is pressed. I've looked around, and every thread is jargon-heavy and not in my context. Please pardon me if this is a "newb' question, or if it's already thouroughly answered somewhere else.
The error I'm getting is "
Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed."
Here's my code
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("myemail", "mypassword");
string to = "toemail";
string from = "myemail";
MailMessage mail = new MailMessage(from, to);
mail.Subject = "test test 123";
mail.Body = "test test 123";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
Any help is greatly appreciated!
The error you're getting could come from a plethora of things from a failed connection, to the server rejecting your attempts, to the port being blocked.
I am by no means an expert on SMTP and its workings, however, it would appear you are missing setting some of the properties of the SmtpClient.
I also found that using port 465 is a bit antiquated, and when I ran the code using port 587, it executed without an issue. Try changing your code to something similar to this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
namespace EmailTest
{
class Program
{
static void Main(string[] args)
{
SendMail();
}
public static void SendMail()
{
MailAddress ma_from = new MailAddress("senderEmail#email", "Name");
MailAddress ma_to = new MailAddress("targetEmail#email", "Name");
string s_password = "accountPassword";
string s_subject = "Test";
string s_body = "This is a Test";
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
//change the port to prt 587. This seems to be the standard for Google smtp transmissions.
Port = 587,
//enable SSL to be true, otherwise it will get kicked back by the Google server.
EnableSsl = true,
//The following properties need set as well
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ma_from.Address, s_password)
};
using (MailMessage mail = new MailMessage(ma_from, ma_to)
{
Subject = s_subject,
Body = s_body
})
try
{
Console.WriteLine("Sending Mail");
smtp.Send(mail);
Console.WriteLine("Mail Sent");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
Console.ReadLine();
}
}
}
}
Tested to work as well. Also of note: if your Gmail account does not allow "Less Secure" apps to access it, you'll get an error and message sent to your inbox stating an unauthorized access attempt was caught.
To change those settings, go here.
Hope this helps, let me know how it works out.
how can i send mass mail through bcc in asp.net(c#)..??
According to the documentation you do it like this:
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an e-mail message from an application very easily.";
MailAddress bcc = new MailAddress("manager1#contoso.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString() );
}
}
You just add several bcc addresses. Note that sending mass mails will easily get you blacklisted and most of your messages will probably end up in spam folders. You should consider using a mass mail service provider for this instead.
You can make you of the System.Net.Mail namespace classes for send out mails in .net applications. Refer to MSDN documentation: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
I am using the SmtpClient in C# and I will be sending to potentially 100s of email addresses. I don't want to have to loop through each one and send them an individual email.
I know it is possible to only send the message once but I don't want the email from address to display the 100s of other email addresses like this:
Bob Hope; Brain Cant; Roger Rabbit;Etc Etc
Is it possible to send the message once and ensure that only the recipient's email address is displayed in the from part of the email?
Ever heard of BCC (Blind Carbon Copy) ? :)
If you can make sure that your SMTP Client can add the addresses as BCC, then your problem will be solved :)
There seems to be a Blind Carbon Copy item in the MailMessage class
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bcc.aspx
Here is a sample i got from MSDN
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an e-mail message from an application very easily.";
MailAddress bcc = new MailAddress("manager1#contoso.com");
//This is what you need
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString() );
}
}
If you are using the MailMessage class, make use of the BCC (Blind Carbon Copy) property.
MailMessage message = new MailMessage();
MailAddress bcc = new MailAddress("manager1#contoso.com");
// Add your email address to BCC
message.Bcc.Add(bcc);