This question has been asked so many times but I am still struggling to find a working solution.
Please consider the below code:
SmtpClient mailClient = new SmtpClient("outlook.office365.com");
MailMessage msgMail = new MailMessage();
msgMail.From = new MailAddress("validUser#domain.com", "displayName#aliasDomain.com");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = new NetworkCredential("validUser#domain.com", "password");
mailClient.EnableSsl = true;
MailAddress sendMailTo = new MailAddress("someValidUser#someValidDomain.com", "Mark Twain")
msgMail.To.Add(sendMailTo);
msgMail.Subject = "Test Subject";
msgMail.Body = "Email content";
msgMail.IsBodyHtml = true;
mailClient.Send(msgMail);
msgMail.Dispose();
When someValidUser - the recipient - receives the email, I want it to show the display name : displayName#aliasDomain.com as opposed to the username registered to the validUser#domain.com account.
How can I achieve this?
Try adding the display name to the headers of the message:
msgMail.Headers.Add("Sender", "displayName#aliasDomain.com");
I hope this helps.
If you reset the from address in the MailMessage object you should be able to send from the alias email address. See below for the full code.
var fromAddress = new MailAddress("root#bobloblaw.com", "Root");
var toAddress = new MailAddress("plaintiff#gmail.com", "Plaintiff");
string fromPassword = "password";
string subject = "Your Lawsuit";
string body = "I regret to inform you...";
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,
From = new MailAddress("bob#bobloblaw.com", "Bob Lob")
})
{
smtp.Send(message);
}
This can work if you simply add an Alias to the Office 365 mailbox. Then use the Alias in the From field and the original username+password for the Credentials.
The recipients will see the name+alias you specify in the From field and not the username for the account. Just tried this with PowerShell Send-MailMessage as well as C#.
Related
I send an email via SMTP google and my code is working fine. What I want is to set this email does not accept replies in the SMTP setting.
Working Code
public ActionResult SendEmail()
{
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#gmail.com", "To Name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "<p>Hello</p>";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
{
smtp.Send(message);
}
return View();
}
Google Mailbox
I don't want the Reply button highlighted in blue
As far as I am aware, it is not possible through SMTP to disable a client's reply function.
Instead, you could create a no-reply address with a vacation responder to alert individuals that the inbox is not being monitored (possibly including where to go if a user does need to reach out to someone).
I am using web mail in my application configuring some credentials in the appsetting.json file. My problem is that registration mails are not arriving.
I dont know how to solve this.
If you use gmail, you can setup like this and sure that Turn On Access for less secure apps by https://support.google.com/accounts/answer/6010255?hl=en-GB
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.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(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
More reference at Sending email in .NET through Gmail
I'm trying to send a simple test email to check how the mail class works in C#, but when trying to call smtp.send(message); I get the following error:
SocketException: No connection could be made because the target machine actively refused it 173.194.207.109:587
I have looked around at the packets in wireshark (although admittedly I don't have the knowledge to make sense of them) and it seems like Google is sending back a [RST, ACK] packet every time I try to send the email. Is there a way to further diagnose this?
var fromAddress = new MailAddress("FROM#gmail.com", "FROM NAME");
var toAddress = new MailAddress("TO#gmail.com", "TO NAME");
const string fromPassword = "password";
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(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Looks like you may want to change your port value because you have EnableSsl = true,
Connect to smtp.gmail.com on port 465, if you're using SSL. (Connect on port 587 if you're using TLS.) Sign in with a Google username and password for authentication to connect with SSL or TLS. Reference
Try:
var fromAddress = new MailAddress("FROM#gmail.com", "FROM NAME");
var toAddress = new MailAddress("TO#gmail.com", "TO NAME");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 465, //or try: port = 587, or port = 25
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
This StackOverflow Answer may also be a good reference to use.
for debugging purposes, I have globally handled all exceptions. Whenever an exception occurs, I silently handle it, and want to send myself an e-mail with the error details, so I can address this issue.
I have two emails, email1#gmail.com, and email2#gmail.com...
I have attempted using this code to send myself an e-mail, but it is not working.
string to = "email1#gmail.com";
string from = "email2#gmail.com";
string subject = "an error ocurred";
string body = e.ToString();
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.google.com");
client.Timeout = 100;
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
I have tried countless other pieces of code but I have no idea how to do it. Does anyone have a solid solution for this? Thanks a bunch.
This has to work. See more info here: Sending email in .NET through Gmail
using System.Net;
using System.Net.Mail;
//...
var fromAddress = new MailAddress("alextodorov01#abv.bg", "From Name");
var toAddress = new MailAddress("kozichka01#abv.bg", "To Name");
const string fromPassword = "fromPassword";
const string subject = "an error ocurred";
const string body = e.ToString();
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
//...
I have write C# code to send mail (my company mail). I tried with gmail and it working but with my company mail is not.
I sure the smtp server is running and port 465 opened since I can send mail by outlook 2k7 with the same account, telnet smtp.domain 465 ok.
When i run the code it throw exception "System.Net.Mail.SmtpException: The operation has time out."
Here is my c# code:
var fromAddress = new MailAddress("ID#domain", "Display Name");
var toAddress = new MailAddress("ID#domain", "Display Name");
const string subject = "Test mail";
const string body = "Test mail";
var smtp = new SmtpClient
{
Host = "smtp.domain",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("ID", "pass"),
Timeout=15000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
try
{
smtp.Send(message);
MessageBox.Show("OK");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Any one know or have same problem that fixed please help me. Thanks so much!
Try changing setting EnableSsl = false in your SmtpClient instance.
You can change this code and reuse this code:
public static void sendEmail(string address, string subject, string message)
{
string email = "yourEmail";
string password = "yourPass";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", portNumber);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}