Click here to Skip to main content
15,884,598 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i am using Microsoft outlook account properties in Asp.net MVC to send mail but problem is that is uses taht account to which i am logged on in my PC, i want to mention FROM in textbox. I read an article which shows the way to integrate code but i don't know how ? thsi piece of code i found.

C#
var application = new Application();
var mail = (_MailItem) application.CreateItem(OlItemType.olMailItem);
mail.To = "anonymous@somedomain.com";
....
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mailItem.SendUsingAccount = account;
mail.Send();


and my code is
C#
public ActionResult dailyLog(String email) 
        {
             email = Request.QueryString["email"];
             try
    {
    // Create the Outlook application.
    Outlook.Application oApp = new Outlook.Application();
    // Create a new mail item.
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    // Set HTMLBody. 
    //add the body of the email
    oMsg.HTMLBody = "Hello, Jawed your message body will go here!!";
    //Add an attachment.
    //String sDisplayName = "MyAttachment";
    int iPosition = (int)oMsg.Body.Length + 1;
    //int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
    //now attached the file
    //Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
    //Subject line
    oMsg.Subject = "Your Subject will go here.";
    // Add a recipient.
    Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
    // Change the recipient in the next line if necessary.

    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(email);
    oRecip.Resolve();
    // Send.
    oMsg.Send();
    // Clean up.
    oRecip = null;
    oRecips = null;
    oMsg = null;
    oApp = null;
                 return View();
     }//end of try block
    catch (Exception ex)
    {
        return View();
    }//end of catch
Posted
Comments
Mehdi Gholam 18-Feb-14 3:21am    
*Bad Idea* to use Office extensions in your web app, since you need to install office on your web server and it is generally not scalable, use the .net System.Net.Mail.MailMessage instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900