How to change email FROM address field? - c#

I was trying to change FROM field with Name and Email address. But When I receive the email, Its shown as below
My network <commity#company.com [My network <commity#company.com]
Mycode is like below
const string from = "My network <commity#company.com>";
StringDictionary headers = new StringDictionary();
headers.Add("to", invitee.userEmail);
headers.Add("subject", ltEmailSubject.Text);
headers.Add("from", from);
headers.Add("content-type", MediaTypeNames.Text.Html);
_emailSuccessful = SPUtility.SendEmail(elevatedSite.RootWeb, headers,
emailBody + Environment.NewLine + "");
I want FROM email address should showup like below
My network [commity#company.com]

SPUtility.SendMail will always use the from smtp address as specified in Central Administration > Outgoing Email Settings and the friendly name is set to the title of the site.
Instead you can use (from http://www.systemnetmail.com/faq/3.2.1.aspx)
MailMessage mail = new MailMessage();
//set the addresses
//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress("me#mycompany.com", "Steve James" );
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");
smtp.Send(mail);

I know this thread is old, but in case someone comes across this as I did, your original code would have worked fine using SPUtility.SendMail if you added quotes around the friendly name in the from address like this:
const string from = "\"My network\" <commity#company.com>";

I believe if you look at the mail object in the .Net Framework, you could do what you wish to do.
Maybe this site could help you out further?

Related

How to find out Out Of Office flag from a mail header using C#

How do I find out "Out Of Office" flag from a received email using C#.
I dont have any scope to parse the Subject / Body. I need to find only thru either the header property if available. Or any other means.
Please suggest.
Thanks,
Sriram
You can add the custom header to the mailMessage as below,
MailMessage mail = new MailMessage();
mail.To = "me#mycompany.com";
mail.From = "you#yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
mail.Headers.Add( "X-Organization", "My Company LLC" );//Your custom header goes here
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );
and letter you can access it as below,
IEnumerable<string> headerValues = mail.Headers.GetValues("X-Organization");
var id = headerValues.FirstOrDefault();
If you are using Outlook Object Model, check if the MailItem.MessageClass property is "IPM.Note.Rules.OofTemplate.Microsoft". This will only work if the sender is in the same domain as the receiver. Otherwise all bets are off - this is nothing special about the OOF messages.

.IsBodyHtml=true causes an empty email body to be sent, but .IsBodyHtml=false sends the body content

I have a WCF service that sends email based on user input. It was brought to my attention that, recently, a particular user's email were being delivered without any body text. If .IsBodyHtml is set to true, no body text is transferred; but, if .IsBodyHtml is set to false, the body has the appropriate text. However, it doesn't seem to be consistent, as it seems to occur only when said user's email address is set as the "From" address.Tech Details:We have an MS Exchange mail server. I'm composing a MailMessage object passing it to the built-in SMTP class to send the message.
The code has been simplified, a bit, for brevity/clarity. Nevertheless, the original code is pretty standard/straight-forward. email refers to a LINQ-to-SQL class object
MailMessage message = new MailMessage();
message.From = new MailAddress(email.fromAddress);
message.To.Add(email.toRecipient);
message.Subject = email.emailSubject;
//set email body
message.IsBodyHtml = true;
message.Body = email.emailBody;
Attachment attachmentFile = null;
if (email.hasAttachment == true)
{
//retrieve attachments for emailID
var attachments = from attach in db.EmailAttachments
where attach.emailID == emailID
select attach;
foreach (var attachment in attachments)
{//attach each attachment
string filePath = Path.Combine(UPLOAD_DIRECTORY, emailID.ToString(), attachment.fileName);
attachmentFile = new Attachment(filePath);
message.Attachments.Add(attachmentFile); //set attachment from input path
}
}
SmtpClient client = new SmtpClient(SMTP_SERVER, SMTP_PORT); //set SMTP server name/URL and port
client.Send(message); //try to send the SMTP email
Since the problem relates to a user, it is probably related to the setting of this user.
Log in as that user and open outlook
Select: File -> Options -> Mail
Scroll down to the section "Message Format"
Probably "Convert to PlainText Format is selected" change this to "Convert to HTML Format"

send plain/text email and getting =0D=0A in email response from server

When i send a email from my site to Uniform http://co.za registrar and cc myself in the mail i get an email returned from them that they received the email, but cannot find some of the information as it has those funny characters added to it. I saw that there was a previous post on the site but none of the answers seem to work for me, and i tried alot of post and blog on google.
The entire body of the email is constructed of the template you get from the registrar site and i used etc as the detail i need to populate and replace those, then send the body of the email off to the registrar.
EX:
1a. Complete domain name: < domainname >
gets replaces with the domain name exmaple.co.za
My Code:
MailMessage emailmsg = new MailMessage("myemail#domain.co.za", "coza-admin#co.za");
emailmsg.Subject = "N domain domain.co.za";
emailmsg.Bcc.Add("myemail#domain.co.za");
emailmsg.Body = Body;
emailmsg.IsBodyHtml = false;
var plainView = AlternateView.CreateAlternateViewFromString(Body, new ContentType("text/plain; charset=iso-8859-1"));
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);
This code send a bcc copy to myself and from the looks of it through outlook all seems fine and i should be but when they receive it the characters gets added. it seems to randomly add those characters. then obviously the request fails as the seconds name server cannot be found.
Returned Email:
Administrative Info
Contact: Administrator=0D=0A 4b. Title/posi= (Administrator)
Company: CompanyName Ltd
Postal: P.O Box 12841, Centrahill, 6006
Phone: +27.00000000=0D=0A 4f. Fax Number: +27.00000000=
Provided Nameserver information
Primary Server : ns1.nameserver.co.za
Secondary 1 : ns2.nameserver.co.za=0d=0a,
Any help would be appreciated.
note: the registrar requires the the email be in plain text and not base64 or html whatsoever as their system picks up plain/text email and faxes and automatically processes them.
Thanks
I figured it out, changed my code to the following:
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);
All characters spaces are kept like they should be in the email template.
Hope this will help someone aswell.
=OD=OA is quoted-printable representation of CrLf.
If you want to use the iso-8859-1 characterset, then you will want to get rid of this line:
plainView.TransferEncoding = TransferEncoding.SevenBit;
If you insist on using 7 bit encoding, then you can only use ASCII characters in your email, so change the is0-8859-1 characterset to "us-ascii"

Sending mail using C#

I have to send mail using C#. I follow each and every step properly, but I cannot send mail using the code below. Can anybody please help me to solve this issue? I know it's an old issue and I read all the related articles on that site about it. But I cannot solve my issue. So please help me to solve this problem. The error is: Failure sending mail. I use System.Net.Mail to do it.
using System.Net.Mail;
string mailTo = emailTextBox.Text;
string messageFrom = "riad#abc.com";
string mailSubject = subjectTextBox.Text;
string messageBody = messageRichTextBox.Text;
string smtpAddress = "mail.abc.com";
int smtpPort = 25;
string accountName = "riad#abc.com";
string accountPassword = "123";
MailMessage message = new MailMessage(messageFrom, mailTo);
message.Subject = mailSubject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Body = messageBody;
message.BodyEncoding = System.Text.Encoding.UTF8;
SmtpClient objSmtp = new SmtpClient(smtpAddress, smtpPort);
objSmtp.UseDefaultCredentials = false;
NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(accountName, accountPassword);
objSmtp.Credentials = basicAuthenticationInfo;
objSmtp.Send(message);
MessageBox.Show("Mail send properly");
If the target mail server is an IIS SMTP (or indeed any other) server then you'll have to check the relay restrictions on that server.
Typically, you either have to configure the mail server to accept incoming relay from your machine's name (if in Active Directory) or IP address. Either that, or you can make it an Open Relay - but if it's a public mail server then that is not recommended as you'll have spammers relaying through it in no time.
You might also be able to configure the server to accept relayed messages from a particular identity - and if this is website code that'll mean that you will most likely have to configure the site to run as a domain user so that the NetworkCredentials are sent over correctly.
oh friends...i got the solution.
just i used the port 26.now the mail is sending properly.
int smtpPort = 26;
anyway thanks to Zoltan
riad.

Sending Email through .NET code

I'm unable to send an email to yahoo server as my code is throwing exception as 'Failure Sending mail' in C# 2008.
Please provide SMTP HostName, PortName for yahoo server and gmail server.
And also kindly provide a good working C# code with which i can send an email directly to any of the mail servers.
Please provide complete working code...so that i will copy into Visual studio environment and execute the same.
As i'm getting exception since morning....unable to resolve the issue.
Kindly help me in this regard.
For Gmail:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount#gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#gmail.com");
mail.To.Add("youraccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
For Yahoo:
var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("youraccount#yahoo.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#yahoo.com");
mail.To.Add("destaccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
Bear in mind that some ISPs (including mine) force their clients to use their SMTP server (as a relay). Spamming protection is the reason.
So you should avoid sending e-mail to the Internet from a client application, unless you give user a chance to specify his SMTP hostname or your app relies on the user's e-mail software (MAPI,...).
Imagine how much easier it would be for us to help you, if you posted the complete exception message, along with a stack trace.
Also, go one step farther and enable logging for System.Net.Mail, so we can see any possible failures at the network level.
If you don't know how to enable logging for SNM, here is a link:
http://systemnetmail.com/faq/4.10.aspx
Thanks!
Dave
There are two ways in which We can send the mail,
1)First is using javascript link "mailTo".This will not send the mail automatically but it will just open the mail window.Refer to the below code
<a class="label" onclick='javascript:buildEmail(this)'>Send Mail</a>
Find below the js method
function buildEmail(el) {
var emailId = Usermail#gmail.com;
var subject="Hi";
var body="Hello";
el.href = "mailto:" + emailId + "?Subject=" + escape(subject) +
"&Body=" + escape(body);
}
2)The second way is to use System.Net.Mail which will automatically send the mail to the recipients in the secured manner.
string subject="Hello";
string body="Data";
using ( MailMessage objMail = new MailMessage ( "Yourmail#gmail.com", "Usermail#gmail.com" ) )//From and To address respectively
{
objMail.IsBodyHtml = false;// Message format is plain text
objMail.Priority = MailPriority.High;// Mail Priority = High
objMail.Body = "Hello";
ArrayList CCarr = new ArrayList();//Assume we add recipients here
// populate additional recipients if specified
if ( ( CCarr != null ) && ( CCarr .Count > 0 ) )
{
foreach ( string recipient in CCarr )
{
if ( recipient != "Please update the email address" )
{
objMail.CC.Add ( new MailAddress ( recipient ) );
}
}
}
// Set the subject of the message - and make sure it is CIS Compliant
if ( !subject.StartsWith ( "SigabaSecure:" ) )
{
subject = "SigabaSecure: " + subject;
}
objMail.Subject = subject;
// setup credentials for the smpthost
string username = "Username";
string passwd = "xxxxxx";
string smtpHost = "mail.bankofamerica.com";
SmtpClient ss = new SmtpClient ();
ss.EnableSsl= true;
ss.Host = smtpHost;
ss.Credentials = new NetworkCredential ( username, passwd );
ss.Send ( objMail );
}
Sigaba Secure Email secures e-mail from client to client through the use of desktop plug-ins and Web-based authentication and decryption.

Categories