I am beginner and don't have any idea to send emails using our own smtp server. I am trying to send Email using my SMTP server. and here is my code
public void SendEmail(string to, string subject, string body)
{
var fromaddr = FromAddress;
var password = Password;
MailMessage msg = new MailMessage
{
Subject = subject,
From = new MailAddress(fromaddr),
Body = body
};
msg.To.Add(new MailAddress(to));
SmtpClient smtp = new SmtpClient
{
Host = SmtpHost,
Port = 25,
UseDefaultCredentials = true,
EnableSsl = false
};
NetworkCredential nc = new NetworkCredential(fromaddr, password);
smtp.Credentials = nc;
smtp.Send(msg);
}
and in SmtpHost i store "localhost". and i have also installed smtp on my web server. and in iis if i choose "store in email directory". i got emails in given directory but if i choose deliver email to smtp server then i won't receive any email in my inbox.
on this option i am giving these values
smtp server : localhost
use localhost : checked
post : 25
authentication settings : not required
can you help me out?
Related
when my asp's service sends email by IEmailSender, sometime google blocks my service. This is my configuration:
public Task SendEmailAsync(string email, string subject, string message)
{
SmtpClient client = new SmtpClient(_configuration["MailSettings:Server"])
{
EnableSsl = bool.Parse(_configuration["MailSettings:EnableSsl"]),
UseDefaultCredentials = false,
Credentials = new NetworkCredential(_configuration["MailSettings:UserName"], _configuration["MailSettings:Password"]),
Port = int.Parse(_configuration["MailSettings:Port"]),
};
MailMessage mailMessage = new MailMessage
{
From = new MailAddress(_configuration["MailSettings:FromEmail"], _configuration["MailSettings:FromName"]),
};
mailMessage.To.Add(email);
mailMessage.Body = message;
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
client.Send(mailMessage);
return Task.CompletedTask;
}
And google make me can not send email sometimes:
When I allow this server, my service can send email normally. After short time, google blocks me again and again.I want my service can send an email everytime no need to check mail and allow this server to use my email account. Please help me. Thanks so much.
I have a web application using ASP.net and C#,in one step it will need
from the user to
send an email to someone with an attachments.
my problem is when the user will send the email i don't want to put their
password every time the user send.
i want to send an email without the password of the sender.
any way to do that using SMTP ?
and this is a sample of my code "not all".
the code is worked correctly when i put my password , but without it ,it
is not work, i need a way to send emails without put the password but
in the same time using smtp protocol.
private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.office365.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "my email";
string password = "******";
string emailTo = "receiver mail";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
// mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
{
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
MessageBox.Show("message sent");
}
}
I believe this can be accomplished easily, but with some restrictions.
Have a look at the MSDN article on configuring SMTP in your config file.
If your SMTP server allows it, your email object's from address may not need to be the same as the credentials used to connect to the SMTP server.
So, set the from address of your email object as you already are:
mail.From = new MailAddress(emailFrom);
But, configure your smtp connection one of two ways:
Set your app to run under an account that has permission to access the SMTP server
Include credentials for the SMTP server in your config, like this.
Then, just do something like this:
using (SmtpClient smtp = new SmtpClient())
{
smtp.Send(mail);
}
Let the configuration file handle setting up SMTP for you. This is also great because you don't need to change any of your code if you switch servers.
Just remember to be careful with any sensitive settings in your config file! (AKA, don't check them into a public github repo)
I am new to azure. My asp .net MVC application hosted in azure. This application has email sending functionality. When the application is moved to azure, email functionality is not working. My error logs displays error as below:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. a65sm9218660oih.6 - gsmtp
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
Code block used to send emails
SmtpClient client = new SmtpClient(GetStringValue(EMAIL_CLIENT), GetIntValue(EMAIL_PORT))
{
Credentials = new NetworkCredential(GetStringValue(EMAIL_USER_NAME), GetStringValue(EMAIL_PASSWORD)),
EnableSsl = sslOn
};
client.Host = SettingsManager.GetStringValue("EmailClient");//smtp.gmail.com
client.Port = SettingsManager.GetIntValue("EmailPort");//587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
Can anyone please help me to resolve this problem. Thank you.
Even I faced the same issue with gmail. Gmail block your emails from azure, because your app is trying to log in from a location with different timezone or different from the one you used to create the account. Check your gmail inbox and it will have an email regarding blocked login attempt.
The solution is to either login to gmail from your azure server or
check the blocked email in your inbox and add that device to verfied
devices. ie select "I recognize this activity as mine" as mentioned in
this link.
This is fully working code from an app we had at one time hosted on Azure. The email sent fine through gmail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Support#domain.com");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(email));
mail.Body = "Email body";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("gmail-email-address", "gmail-password");
smtp.EnableSsl = true;
smtp.Send(mail);
First check your code, there is issues on port, credentials, timeout, ...
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, "yourApplicationSpecificPassword"),
Timeout = 10000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
IsBodyHtml = true,
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Then in gmail setting, use 2 step verification -
https://myaccount.google.com/u/3/signinoptions/two-step-verification
and generate app passwd - https://myaccount.google.com/u/3/apppasswords
use generated passwd in code "yourApplicationSpecificPassword"
good luck!!
I am using the code as described in this question. However get following error when send an email.
Mailbox unavailable. The server response was: please authenticate to
use this mail server
Any ideas what could be wrong?
UPATE: Here is the code
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();
MailMessage Message = new MailMessage("From", "To", "Subject", "Body");
Client.Send(Message);
With following in App.config.
<system.net>
<mailSettings>
<smtp from="support#MyDomain1.com">
<network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" />
</smtp>
</mailSettings>
</system.net>
The code posted there should work. if it doesn't, you might try setting the username and password in code-behind rather than reading them from the web.config.
Code sample from systemnetmail.com:
static void Authenticate()
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret");
smtp.Send(mail);
}
Yes, the smtp server is telling you that in order to relay email for you, you need to authenticate before attempting to send the email. If you have an account with the smptp server, you can set the credentials on the SmtpClient object accordingly. Depending on the authentication mechanism supported by the smtp server, the port, etc, will be different.
Example from MSDN:
public static void CreateTestMessage1(string server, int port)
{
string to = "jane#contoso.com";
string from = "ben#contoso.com";
string subject = "Using the new SMTP client.";
string body = #"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient(server, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
ex.ToString() );
}
}
The bottom line is that your credentials are not being passed to the Smtp server or else you wouldn't be getting that error.
In this post Sending Email in .NET Through Gmail we have a code to send email through gmail, in the send mail we find from Field contain gmail account that I used
I use the same code but by changing the From Address to any email I want ans set gmail address in Credentials as bellow
var fromAddress = new MailAddress("AnyEmai#mailserver.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 smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("from#gmail.com", fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
But in the sent email gmail account still appear in From Address and AnyEmai#mailserver.com not appear ... is there any way to do that ?
It's that way by design. You have to find another way to send outbound emails so that the return address you want shows up (I've been there, there seems to be no way to spoof the from address).
Shall you check this question change sender address when sending mail through gmail in c#
I think it is related to your inquiry.
You can import an email id in your gmail account using Mail Settings >> Accounts and Import options and that can be used for sending the mails, however if you are want to use some random email id everytime to send the mails it is not possible. Gmail will treat that as a spoofing/spam and it will reset the mail address to your original mail id before sending the mail.
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("from#gmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Your Subject";
mail.Body = "Body Content goes here";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/file.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("from#gmail.com", "mailpassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
There are many other mail services from which you can achieve the same but not through the gmail. Checkout the blog Send email in .NET through Gmail for sending mail using different properties.
The email address needed to be verified by gmail from the account settings.
Please find my blog post for the same describing it in detail, the steps to be followed:
http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
before following all the above steps, you need to authenticate your gmail account to allow access to your application and also the devices. Please check all the steps for account authentication at the following link:
http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html