ASP.NET sending with Gmail smtp overrides From Address - asp.net

I was trying to send from a aspx page a mail message using the smtp gmail (we are google apps customers and I tried with a paid account and with a free gmail...)
I was able to send the messagge setting credentials, port, ssl, etc. but what is wrong is the From address of the message. Indeed, the mail is coming always from the address of the auth user and not from what I set in the msg.From property...
Eg. the mail arrives with from "myaccount#gmail.com" and not from sender#somedomain.com
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sender#somedomain.com", "sender name");
msg.To.Add(new MailAddress("recipient#somedomain.com","recipient name"));
msg.Subject = "subject";
msg.Body = "some body";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myaccount#gmail.com", "*******");
smtp.EnableSsl = true;
smtp.Send(msg);
Is there a setting to avoid this ?
thanks in advance
Sandro

Check the message headers.
Depending on your email client, here's the instructions how to reach them: https://support.google.com/mail/answer/22454?hl=en
You'll see, that FROM field is only for indication of the sender's name.
It can be ignored by email client, especially, if it contains address different than real sender's address.

Related

SMTP server: too many connections and service not available

Hello my name is Prince...im trying to implement a register module to send a confirmation email to my client email address in asp.net. I'm using SMTP client to implement this. I used the following code:
using (SmtpClient smtp = new SmtpClient())
{
using (MailMessage mm = new MailMessage("onukwilip#gmail.com", Email.Text.Trim()))
{
mm.Subject = "Dummy";
mm.Body = "Body";
mm.IsBodyHtml = false;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential credit = new NetworkCredential("onukwilip2006#gmail.com", "onukwilip2006+_");
smtp.UseDefaultCredentials = true;
smtp.Credentials = credit;
smtp.Port = 587;
smtp.Send(mm);
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"Mail sent successfully\");", true);
i used the above function in my button onclick, but when i send the smtp is telling me:
Service not available, closing transmission channel. The server response was: Service not available
I've set my settings in account.google.com to allow less secure apps or something like that but the smtp is still not working.
It was telling me the below text the night before.
Service not available, closing transmission channel. The server response was: Too many connections
But now its telling me the below code this night.
Service not available, closing transmission channel. The server response was: Service not available
I also tried using the below function, but its still not working:
using (MailMessage mm = new MailMessage("onukwilip#gmail.com", Email.Text.Trim()))
{
mm.Subject = "Dummy";
mm.Body = "Body";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential credit = new NetworkCredential("onukwilip2006#gmail.com", "onukwilip2006+_");
smtp.UseDefaultCredentials = true;
smtp.Credentials = credit;
smtp.Port = 587;
smtp.Send(mm);
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"Mail sent successfully\");", true);
Ive changed the port to : 25, 465,587 but still gives me the same error, when i set to 465 it tells me
Failure to send mail
Ive checked google for some answers but none seems to accomplish or settle the problem. Please help me...
UPDATE: I just tried it again using smtp.gmail.com as the host and port 587. but its still telling me:
Service not available, closing transmission channel. The server response was: Server busy, too many connections

Error loading in ASP.NET Email verification URL at localhost

I am using smtp gmail to for email verification. It is assigned as per the standards. When a new user registers, it is able to send mail to user along with URL for verification. But on clicking the URL which the user receives, it is unable to load the page. What might be the error
the URl address is like
string url = "http://localhost:49432/EmailVerification.aspx?vcode=" + id
this is what my smtp class mail looks like
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Port = 587; // 465; //Gmail works on this port
// smtp.Port = 25;
smtp.Timeout = 100000;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.Send(objmail);
//ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MSG", "alert('Mail Sent Sucessfully');", true);
in the webconfig file. I have given

Gmail message is rejected

I am trying to send emails from ASP.net project using "System.Net.Mail.MailMessage". I need to add about 30 contacts to "To" or "BCC" list.
It works for few recipients but when the number increases;
Gmail give error:
"Message rejected. See http://support.google.com/mail/bin/answer.py?answer=69585 for more information."
And block my accont for sending any more emails for 1 day.
How can i allow gmail to send email to many recipients?
If not what is the alternative way?
You should be able to comfortably send to 30 addresses.
I tried 40 email addresses for CC list (worked for To list as well) with the following test code,
string mailFrom = "test123#gmail.com";
string mailTo = "testccc#domain.com"; //One of your test emails
MailAddress to = new MailAddress(mailTo);
MailAddress from = new MailAddress(mailFrom);
MailMessage mail = new MailMessage(from, to);
mail.Subject = "Test subject";
mail.Body = "Hello world!!!!";
//Keep adding your emails as follows, do it in a for loop
mail.CC.Add("dhanuka1#domain.com");
mail.CC.Add("dhanuka2#domain.com");
...
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mydev#gmail.com", "password");//Your credentials
smtp.EnableSsl = true;
Console.WriteLine("Sending email...");
smtp.Send(mail);
Console.WriteLine("Email sent...");
Console.ReadLine();

How Can i Send Mail Through Exchange Server by using SMTP

I want to Run Below code without
NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD").
BY using Only Exchange Host (Server Name) And Port
Im Getting Error For this code : Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender
protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient("ExchangeServerName",25);
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("bala#OfficeName.com", "From Me");
MailAddress toAddress = new MailAddress("bala#OfficeName.com", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.UseDefaultCredentials = true;
System.Net.NetworkCredential nc = CredentialCache.DefaultNetworkCredentials;
smtpClient.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential("ExchangeServerName", 25, "Basic");
smtpClient.Send(message);
lblText.Text ="Email sent.";
}
catch (Exception ex)
{
lblText.Text = "Coudn't send the message!\n " + ex.Message;
}
}
I have done it. For more details about my code use this link.
Below Code is worked Fine with
Server : Windows Server 2003,Windows Server 2008,Windows Server 2008 R2
IIS : 6.0, 7.0
.Net Frame Wotk : 2.0,3.5,4.0
string sMessage;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//you can provide invalid from address. but to address Should be valil
MailAddress fromAddress = new MailAddress("bala#technospine.com", "BALA");
smtpClient.Host = "Exchange Server Name";
smtpClient.Port = 25;
//smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
message.From = fromAddress;
message.To.Add(bala#technospine.com); //Recipent email
message.Subject = _subject;
message.Body = _details;
message.IsBodyHtml = true;
//smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
sMessage = "Email sent.";
}
catch (Exception ex)
{
sMessage = "Coudn't send the message!\n " + ex.Message;
}
lblMailStatus.Text = sMessage;
You are attempting to send a mail message using Exchange. In order to do that, the sender (or sending process) must have permissions on the account it is logged in under to send on behalf of the user you are specifying as the sender. This is different from going through Exchange's SMTP mail transfer agent (MTA) in order to have Exchange receive and route an email message. So you are on the right track with knowing you should do this using SMTP, but you are just trying to use the wrong API for accomplishing this. You want to take a look at CDOSYS for sending it through the SMTP MTA without having to do user authentication. Search on System.Web.Mail.MailMessage for more specific examples - there are plenty out there. If the Exchange server does not seem to accept/deliver the SMTP message delivered to it in this fashion, you might simply need to open up its configuration a bit. In that event, the Exchange server is probably configured with tight security on routing of mail received via its SMTP MTA and just needs to have the IP address of the machine(s) you are sending these messages from configured to allow for mail forwarding.
try NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD","DOMAIN")

How to send mail for .org domain using web mail service in asp.net

I want to send email from my asp.net application
I have successfully sent the email form my aspx page for .com domain
by the following code
MailMessage msg = new MailMessage();
msg.From = new MailAddress("support#mydomain.com");
msg.To.Add("recipiant#gmail.com");
msg.Subject = "Demo Subject";
msg.Body = "Hey Test email";
SmtpClient client = new SmtpClient();
client.Host = "mail.mydomain.com";
client.Port = 25;
client.Credentials = new NetworkCredential("username", "password");
client.Send(msg);
But in case of .org domain it is not going email
I have used the below code for .org domain
MailMessage msg = new MailMessage();
msg.From = new MailAddress("support#mydomain.org");
msg.To.Add("recipiant#gmail.com");
msg.Subject = "Demo Subject";
msg.Body = "Hey Test email";
SmtpClient client = new SmtpClient();
client.Host = "mail.mydomain.org";
client.Port = 25;
client.Credentials = new NetworkCredential("username", "password");
client.Send(msg);
In user name filed i have entered only user name not with domain name.
so first sent when i have used for .com domain
but when i have used .org mail with correct id and password, then one error is throwing me, called:
Mailbox unavailable. The server response was: Authentication is required for relay
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: Authentication is required for relay
Try testing the SMTP settings using something like Telnet, that'll tell you if you've got the right details.
Try something like this http://support.microsoft.com/kb/153119

Resources