C# Outlook Interop Send from Folder - c#

I'm attempting to send an email from a e-mail address that's listed as a folder. Basically I have a folder with an e-mail address assigned to it. Whenever something comes to that email it goes to the folder. The E-Mail Address is not an account assigned to me. I would use SMTP But our corporate network does not allow this.
How can I send an e-mail in C# from this Folder's E-Mail?
My code is setup as follows.
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, true, true);
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.Subject = subject;
string html;
html = message;
html = html.Replace("\n","<br/>");
oMsg.HTMLBody = html;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(to);
//Rest of my closing stuff here.

If you already have the folder's email address (you don't mention if this is part of the problem but it sounds like it is not) you shouldn't have to use Outlook interop for this. Try the classes in System.Net.Mail. This site has some good examples, but here's something quick:
const string PR_SMTP_ADDRESS =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
...
var msg = new MailMessage();
msg.From = new MailAddress(recipient.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS).ToString());
msg.To.Add(new MailAddress(folderAddress));
msg.Subject = subject;
msg.IsBodyHtml = true;
msg.Body = html;
var smtpClient = new SmtpClient("{SMTP server address or IP}");
smtpClient.Send(msg);
I'm only guessing about the part where I get the recipient address, it's based on this MSDN page.

Seems to me the whole folder thingy is irrelevant to your problem (correct me if I'm wrong there), and all it comes down to is you want to send an e-mail through Outlook with a specific reply address. You can use MailItem.SenderEmailAddress for that purpose:
oMsg.SenderEmailAddress = "my.special.address#domain.com"
As an alternative, you could add the reply address to the MailItem.ReplyRecipients collection.

Related

Sending email using CDO

I have a database which contains email address of recipients and a flag column. If the flag is "YES", a mail will be sent to that user. I want to accomplish this using CDO in my ASP.NET project. So, what may be the favorable routine for doing this. I am new to CDO.
What I tried as a beginner is this:
CDO.Message oMsg = new CDO.Message();
CDO.IConfiguration iConfg;
iConfg = oMsg.Configuration;
ADODB.Fields oFields;
oFields = iConfg.Fields;
ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
oFields.Update();
oMsg.Subject = "Test CDO";
oMsg.From = "abc#xyz.com";
oMsg.To = "someone#example.com";
//oMsg.TextBody = "CDO Mail test";
oMsg.HTMLBody = "CDO Mail test";
oMsg.Send();
Response.Write("Mail Sent");
But its not helping me what I want.

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.

How to change email FROM address field?

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?

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.

Receiving Emails on Hosting Domain Email Id from any user visiting to site

I am trying to create a contact us page where visitor of the site can leave a message to site-admin.
say I m running this site
www.example.com
and i have an id contact#example.com
Now a new visitor visits my site and fills up the contact us form.
He fills
FROM - abc#yahoo.com
Subject- Query
Message- My question....
I want to send this message as a mail to contact#example.com
And on successfull sending of mail i want to send this user autogenerated mail as an acknowledgement.
What I need to do for this, My hosting server is GoDaddy
I am trying the following code.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(txtEmailId.Text);
mailMessage.To.Add(new MailAddress("contact#example.com"));
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = txtMessage.Text;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient("relay-hosting.secureserver.net");
//sc.Credentials = new System.Net.NetworkCredential("contact#example.com", "MyPassword");
sc.Send(mailMessage);
}
catch (Exception ex)
{
Label1.Text = "An Error has occured : "+ex.Message;
}
This error occurs while trying to send mail.
Mailbox name not allowed. The server
response was: sorry, your mail was
administratively denied. (#5.7.1)
It looks like from your code you are specifying that the mail message's From field is based on the email address the user fills in. The mail server mail be rejecting this and only allowing email from the domain name example.com or perhaps the server name itself. I've run into this before where your From field must be the original site domain...
If you want to have the ability for the form-mail recipient to directly reply, use the ReplyTo field rather than the from:
MailMessage mailMessage = new MailMessage();
//try using a domain address that matches your server and/or site.
//the email address itself may also have to exist depending on the mail server.
mailMessage.From = new MailAddress("no-reply#example.com");
//set the replyto field
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
//send the mail...
Hope this might help...
EDIT:
After you've edited your question, the above is still relevant. It's very likely you cannot use a From address outside of your own site domain example.com. If you want to send two copies of the request (one to contact#example.com and the other to the user as an acknowledgment), you simply need to add to the To property:
MailMessage mailMessage = new MailMessage();
//try using a domain address that matches your server and/or site.
//the email address itself may also have to exist depending on the mail server.
mailMessage.From = new MailAddress("abc#example.com");
//add the contact email address to the To list
mailMessage.To.Add(new MailAddress("contact#example.com"));
//add the user email address to the To list
mailMessage.To.Add(new MailAddress(txtEmailId.Text));
//set the replyto field
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
//send the mail...
If you need to add a simple message for the user acknowledgment email you could also do:
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("abc#example.com");
mailMessage.To.Add(new MailAddress("contact#example.com"));
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = txtMessage.Text;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient("relay-hosting.secureserver.net");
//sc.Credentials = new System.Net.NetworkCredential("contact#example.com", "MyPassword");
//send to only the contact#example.com first
sc.Send(mailMessage);
mailMessage.To.Clear();
mailMessage.To.Add(new MailAddress(txtEmailId.Text));
//add a simple message to the user at the beginning of the body
mailMessage.Body = "Thank you for submitting your question. The following has been submitted to contact#example.com: <br/><br/>" + mailMessage.Body;
//send the acknowledgment message to the user
sc.Send(mailMessage);
}
catch (Exception ex)
{
Label1.Text = "An Error has occured : "+ex.Message;
}
It is probably Serverfault.com question, any how
you need to check these options
Admin Home -> Configuration -> Email
Options -> - E-Mail Transport Method =
sendemail
reference 553 sorry, your mail was administratively denied. (#5.7.1)

Categories