I'm trying to send emails using gmail's username and password in a Windows application. However, the following code is sending the mail to only the first email address when I collect multiple email address in my StringBuilder instance.
var fromAddress = new MailAddress(username, DefaultSender);
var toAddress = new MailAddress(builder.ToString());//builder reference having multiple email address
string subject = txtSubject.Text;
string body = txtBody.Text; ;
var smtp = new SmtpClient
{
Host = HostName,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(username, password),
//Timeout = 1000000
};
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = chkHtmlBody.Checked
};
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
message.Attachments.Add(attechment);
}
if(this.Enabled)
this.Enabled = false;
smtp.Send(message);
What am I doing wrong, and how can I sort out my problem?
Best bet is to message.To.Add() each of your MailAddresses individually. I think early versions of .Net were happier to parse apart comma or semicolon separated email addresses than the more recent runtime versions.
I was having the same problem.
The code is actually
message.To.Add("xxx#gmail.com, yyy#gmail.com");
this one can work in .net 3.5
if you use
message.To.Add( new MailAddress("xxx#gmail.com, yyy#gmail.com"));
this won't work in .net 3.5
Related
I'm sending e-mails with System.Net.Mail.SmtpClient. The problem is that when I add an attachment to the MailMessage the received e-mails subject and the attached filenames decoding goes wrong.
When the subject or attachment name string contains only one special character like 'ü' the decoding fails, but if it contains more like 'ÍŰÁÉÚŐÓÜÖíűáéúőóüö' the decoding is ok.
Code to reproduce the problem:
var message = new MailMessage();
message.From = new MailAddress("test#test.com", "test");
message.To.Add(new MailAddress("test#test.com", "test"));
message.Subject = "Teszt üzenet";
message.Body = "Teszt üzenet";
// if i remove the line below en/decoding is ok
message.Attachments.Add(new Attachment(#"document.pdf") { Name = "fájl.pdf" });
using (var smtp = new SmtpClient() {
DeliveryFormat = SmtpDeliveryFormat.International,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = Settings.UseSsl,
Host = Settings.Host,
Port = Settings.Port,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(Settings.Username, Settings.Password)
}) {
smtp.Send(message);
}
Setting message.SubjectEcoding and others doesn't seem to matter. The default is UTF-8 which i would use anyway.
I'm using gmail smtp for testing and this is part of a webapi 2 project.
The reason for the encoding/decoding failures was the gmail smtp.
Having researched the issue extensively especially applying recommendations from similar issues on stackoverflow, the below code still returns error ""System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: connection rejected at....""
try
{
var mail = new MailMessage();
mail.From = new MailAddress("demo77377#gmail.com");
mail.To.Add(new MailAddress("xxxxxxx#gmail.com"));
mail.Subject = "TEST";
mail.Body = "This is a test mail from C# program";
using (var smtp = new SmtpClient())
{
smtp.Credentials = new System.Net.NetworkCredential("demo77377#gmail.com", "AABBCCDDEE1!","gmail.com");
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Timeout = 10000;
//
smtp.Send(mail);
Console.WriteLine("Message sent successfully");
Console.ReadLine();
}
}
catch (Exception e)
I have done everything possible
On Client> I have alternated smtp properties (permutation) etc
On Server> I have made gmail account less secure, I have disabled captcha etc
I observed that similar issues on stackoverflow were mostly dated over 3years ago and thus, is it possible that gmail no longer supports this SMTP method via C#, likewise has it been deprecated in favor of gmail API
Also, please find provided in code, original password supplied for the gmail account, in order to confirm if this issue is general or isolated to this gmail account
Thanks
This code is verified to work fine:
var fromAddress = new MailAddress("YOUREMAIL#gmail.com", "YOUREMAIL#gmail.com");
var toAddress = new MailAddress("YOUREMAIL#gmail.com", "YOUREMAIL#gmail.com");
const string fromPassword = "YOURPASSWORD";
const string subject = "YOUREMAIL#gmail.com";
const string body = "YOUREMAIL#gmail.com";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
I'm not comfortable including your personal details even though you have so I have removed them. Obviously replace the email and password with the correct details.
Finally got it working after switching from my hotel's wireless network to office network...type of network connection has a role to play in sending email via SMTP in C#
I'm using SmptClient to send email from a gmail account. This account sends lots of emails every day (sort of a support#mydomain.com automatic emails)
All was working fine until it suddenly stopped working. I didn't change anything in my application nor did i deploy a new version.
When i try a regular SmptClient example code it gives me this error message:
Unable to read data from the transport connection: net_io_connectionclosed.
If i change only the fromAddress and fromPassword to a different account, without changing anything else in the code, the code works!
Here's the sample code which i'm using: (The code if fine becuase it does work for a different account)
var fromAddress = new MailAddress("****", "****");
var toAddress = new MailAddress("****", "****");
const string fromPassword = "****";
const string subject = "testing... ";
const string body = "body test";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
};
smtp.Send(message);
When i connect to gmail using the invalid account and send an email, it does work.
It seems as if the SMTP server is failing to authenticate.
I connected to the list of Gmail Latest Account Activity and it didn't show the connection attempts i made from code. Please help, Thanks!
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
I have a SMTP server that only accepts a predefined From sender.
However, I can add a custom from header in the DATA structure to set another from (sender ) address. This is possible if I test using Telnet to compose an email message:
>helo there
>mail from:the.only.allowed.sender#mydomain.com
>rcpt to:magnus#mydomain.com
>data
From:magnus#mydomain.com
To:some.user#mydomain.com
Subject:Test
Test message
.
When this email has arrived at the recipient, the from address is magnus#mydomain.com, which is the goal.
Here's my problem.
How can I mimic this "from header" in the System.Net.Mail SMTP class?
Setting the from property fails, because that would violate the SMTP server policies.
Something like this would be great, but it doesn't work:
var fromAddress = new MailAddress("the.only.allowed.sender#mydomain.com");
var toAddress = new MailAddress("user#mydomain.com");
string subject = "Subject";
string body = "Body";
var smtp = new SmtpClient
{
Host = "my-smtp-server",
Port = 25,
DeliveryMethod = SmtpDeliveryMethod.Network
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
ReplyTo = new MailAddress("magnus#mydomain.com"),
})
{
message.Headers.Add("From", "magnus#mydomain.com"); // <---- This would be great, if it worked
smtp.Send(message);
}
Has anybody got any ideas?
PS. Writing a custom SMTP class myself, using TCP sockets, it works, but can this be done in the standard .NET classes?
Well, I should have done some experimenting before posting the question...
(But instead of deleting it, I'll leave it here if others would have the same issue).
The solution was to set both the From and Sender properties on the MailMessage object.
(I'd need to set both, otherwise it doesn't work):
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
From = new MailAddress("magnus#mydomain.com"),
Sender = new MailAddress("the.only.allowed.sender#mydomain.com")
};
smtp.Send(message);