WebMail.Send(to: "someone#somewhere.com", subject: "email subject", body: "message body, isBodyHtml: false );
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "me#mydomain.com";
WebMail.Password = "the password";
WebMail.From = "me#mydomain.com";
By using the above method, I am receiving email from sender name as "me#mydomain.com".. I tried changing the parameters "userName" and "From", but the gmail server rejects the message.
What parameter to add to receive email from a sender name instead of email id..
try this:
System.Web.Mail is not a full .NET native implementation of the SMTP
protocol. Instead, it uses the pre-existing COM functionality in
CDONTS. System.Net.Mail, in contrast, is a fully managed
implementation of an SMTP client.
So In MailAddress From = new MailAddress(FromName, FromName);
^
From = Gets or sets the e-mail address of the sender
Source
Related
We have several asp.net web apps that send emails, and the MailMessage object is configured with an SMTP server, username and password. The emails are sent with no problems.
In an SSIS package, I added an SMTP connection manager, and I configured the smtp server. I set UseWindowsAuthentication=True because I don't see where I type in username/password.
When I run the package from SQL Server Agent, the SSIS sends the email correctly, so apparently, the user/password is not needed.
So how can the SMTP package send an email without the user credentials? Does it make sense that the asp.net don't need the credentials either?
We're all under the same company network and we use Exchange Server.
Thanks.
Create a SMTP Connection Manager with a parameterized ConnectionString property with a string which contains the smtp user and password.
Create connection using New Connection... option selecting SMTP as type.
Save without any connection settings. Give it any name you want.
Right click the connection and select Parameterize...
Select Property = ConnectionString
Select Create new parameter (e.g. SMTPConnectionManager_ConnectionString)
Set Value to connection string (e.g. SmtpServer=aspmx.l.google.com; port=25; UseWindowsAuthentication=False;EnableSsl=False; user=user#gmail.com; password=password123)
Set scope at appropriate level for your deployment method (Package or Project).
Click OK
Check out this link.
It explains that the package is using the Sql Server Agent account to connect to the host.
Furthermore, the SMTP connection manager supports only anonymous authentication and Windows Authentication. It does not support basic authentication - as stated in the documentation.
The answer from Alan Gaylor didn't work for me, but doing the following in a script task, not an email task, worked:
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
public void Main()
{
string UserName = Dts.Variables["UserName"].Value.ToString();
string Password = Dts.Variables["Password"].Value.ToString();
string EmailRecipient = Dts.Variables["EmailRecipient"].Value.ToString();
string EmailSender = Dts.Variables["EmailSender"].Value.ToString();
string SMTPEndPoint = Dts.Variables["SMTPEndPoint"].Value.ToString();
Int32.TryParse(Dts.Variables["SMTPPort"].Value.ToString(), out int SMTPPort);
string MessageSubject = Dts.Variables["MessageSubject"].Value.ToString();
string MessageBody = Dts.Variables["MessageBody"].Value.ToString();
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(EmailRecipient));
msg.From = new MailAddress(EmailSender);
msg.Subject = MessageSubject;
msg.Body = MessageBody +
"\n" +
"\n" +
"DISCLAIMER: The information contained in this transmission may contain privileged and confidential information. " +
"It is intended only for the use of the person(s) named above.If you are not the intended recipient, " +
"you are hereby notified that any review, dissemination, distribution or duplication of this communication " +
"is strictly prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.";
SmtpClient client = new SmtpClient(SMTPEndPoint, SMTPPort)
{
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(UserName, Password)
};
try
{
client.Send(msg);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
Follow Below steps
Create a Send Mail Task, then create a new smtpConnection.
Type your Mail server name and click OK
Right-click on the SMTP Connection Manager and click Parameterize.
Select ConnectionString from the property list
Add username, password and port to your connection string value
SmtpServer=mail.yourServerName.com;UseWindowsAuthentication=False;EnableSsl=False;port=portnumber;user=YourUserName;Password=YourPassword;
I am producing a website for a friends company and they would like customers to be able to contact them using a form on the website that will email the query to their work email address.
All the examples I have been able to find use GMail's SMTP server. I have managed to get this to work using the code below but e-mails always appear in the inbox as from the GMail account, regardless of what I set mm.From to be. I understand that this is the downside to using GMail's SMTP server.
Dim mm As MailMessage = New MailMessage()
mm.From = New MailAddress("customer#test.com")
mm.Subject = "Test Email"
mm.Body = "<p>This is a test email</p>"
mm.IsBodyHtml = True
mm.To.Add(New MailAddress("info#myCompany.com"))
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New System.Net.NetworkCredential()
NetworkCred.UserName = "mycompany#gmail.com"
NetworkCred.Password = "myPassword"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
Try
smtp.Send(mm)
Catch ex As Exception
Response.Write(ex)
End Try
I would like the senders e-mail address to appear in the from field so users can just click reply when a message comes in.
The business e-mail account they have is provided by GoDaddy and I have tried entering the SMTP and account details in the code but I get the following error:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
Any help would be greatly appreciated.
Thanks
You can use any SMTP service that you have access to. So if GMail's doesn't do what you need, try another one. (The settings for that other one may be different, check with the service host for details.)
However, one thing you can definitely do in your code is set a ReplyTo address. From is only one header in the message, there are others that can be used. A ReplyTo header tells the email client that replies should be sent to a given email address.
mm.ReplyTo = New MailAddress("customer#test.com")
EDIT: I'll add that the domain is hosted by Web.com. Perhaps someone else uses them and has experienced something similar.
The code below is set up for both my own smtp server and gmail. Both fail for different reasons. I'm writing this in VS 2010/.Net 4
When I send from my own domain smtp server I receive no exceptions I just never send any email. I have tried sending from different accounts to different accounts. There are no exceptions, is just never gets delivered. I changed my password and did receive and authentication error, which seems to prove I had the credentials correct. I am following my domain hosts instructions for domain name and port.
With gmail I get the 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". I read about this and followed the links to allow less secure devices to access my gmail. I received the confirmation email from gmail indicating I had made the changes for less secure email, but I still get the same error.
I have read and tried everything at the following links and can't get past this.
http://ithoughthecamewithyou.com/post/sending-email-via-gmail-in-cnet-using-smtpclient
c# SmtpClient class not able to send email using gmail
http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
When looking at the code I am using, does anything jump out at you and being not right? Is there a property set wrong or am I missing a Property?
Public Shared Sub SendMailMessage(ByVal sfrom As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(sfrom)
mMailMessage.To.Add(New MailAddress(recepient))
If Not bcc Is Nothing And bcc <> String.Empty Then
mMailMessage.Bcc.Add(New MailAddress(bcc))
End If
If Not cc Is Nothing And cc <> String.Empty Then
mMailMessage.CC.Add(New MailAddress(cc))
End If
mMailMessage.Subject = subject
mMailMessage.Body = body
mMailMessage.IsBodyHtml = True
mMailMessage.Priority = MailPriority.Normal
'GMAIL
'Dim mSmtpClient As New SmtpClient()
'mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
'mSmtpClient.UseDefaultCredentials = True
'mSmtpClient.EnableSsl = True
'mSmtpClient.Host = "smtp.gmail.com"
'mSmtpClient.Port = 587
'mSmtpClient.Credentials = New System.Net.NetworkCredential("my gmail address", "my password")
'mSmtpClient.Send(mMailMessage)
'MY DOMAIN
Dim mSmtpClient As New SmtpClient()
mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
mSmtpClient.UseDefaultCredentials = True
mSmtpClient.EnableSsl = False
mSmtpClient.Host = "smtp.mydomain.com"
mSmtpClient.Port = 587
mSmtpClient.Credentials = New System.Net.NetworkCredential("my full email address", "my password")
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
'HttpContext.Current.Response.Redirect("~\EmailError.aspx")
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
End Sub
For gmail my password was too weak. Ugh! Why couldn't Google just tell me that instead of saying, "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"
It was an authentication error, so the message was accurate, just misleading. 6 hours of my life I'll never get back. Oh, plus the useless half hour I spent on the phone with Web.com support.
This is intranet application and I am using company's mail realy to send emails. It works fine in other applications I have but not in this one and everything is the same.
I get "System.Net.Mail.SmtpException: Syntax error in parameters or arguments. The server response was: 5.5.2 MAIL FROM syntax error" when attempting to send mail.
This is how I set it up:
MailMessage mm = new MailMessage();
mm.From = new MailAddress("NoReply#AMS_Redirection.company.org");
mm.To.Add(new MailAddress("my_emailaddress#company.org"));
mm.Subject = "AMS Redirection Error";
mm.Priority = MailPriority.Normal;
mm.IsBodyHtml = true;
mm.Body = "some html stuff here";
SmtpClient sc = new SmtpClient("mailrelay.company.org");
sc.Credentials = new NetworkCredential();
try
{
sc.Send(mm);
}
catch (SmtpException smtpEx)
{
//A problem occurred when sending the email message
string sScript = string.Format("alert(\"There was a problem in sending the email: {0}\");", smtpEx.Message.Replace("\r\n", " "));
ClientScript.RegisterStartupScript(this.GetType(), "SMTP Error", sScript, true);
}
I can successfully ping the mail relay.
Try setting the 'from' address to something you KNOW is wrong. I get this exception when the 'from' address is rejected by my SMTP forwarding service. If it still fails the same way you know the problem is in the config of the relay.
did you check if the smtp port of your SMTP server is the standard port? Otherwise you have to explicitly set it.
I am trying to send email via System.Net.Mail. On clicking send I am getting the following exception
System.Net.Mail.SmtpFailedRecipientException: Mailbox name not allowed. The server response was: We do not relay non-local mail
MailAddress toAddress = new MailAddress(toEmail);
MailAddress fromAddress = new MailAddress(fromEmail);
MailMessage mailMsg = new MailMessage(fromAddress, toAddress);
mailMsg.Subject = EmailSubject;
mailMsg.Body = MessageBody.ToString();
mailMsg.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new SmtpClient(EmailSettings.SmtpServer);
smtp.Send(mailMsg);
That is all I am doing.
What workaround should I take for this to work
You should authenticate your SMTP client using credentials AND sender mailbox belonging to SMTP server you're connecting to.
Also (depending on your mail server) the fromAddress needs to be an actual account on the mail server.