简体   繁体   中英

Sending an outlook email from client computer running a program installed on the server

Good day to you all

I have a java web application running over a network and the application is installed on the server.As part of the the program it sends outlook emails automatically, I used java com bridge(jacob) to achieve that. The emails are being send on the server , I would like to send outlook emails on the client computer so that the users can keep track of the emails they send on their client computer.

I am using this code

 public class EmailAPI
    {
        private ActiveXComponent ol;
        private Dispatch outlook;
        private Object mapi[] = new Object[1];
        private Object email[] = new Object[1];

        public EmailAPI()
        {
            mapi[0] = "MAPI";
            email[0] = 0;

            ol = new ActiveXComponent("Outlook.Application");
            outlook = ol.getObject();
            Dispatch.call(outlook,"GetNamespace",mapi).toDispatch();
        }

        public void createEmail(String receiver,String cc,String subject, String body, String attachments[])
        {
            Dispatch mail = Dispatch.call(outlook,"CreateItem",email).toDispatch();
            //Dispatch mailItem = Dispatch.call(axOutlook, "CreateItem", 0).getDispatch();
            Dispatch inspector = Dispatch.get(mail, "GetInspector").getDispatch();  
            Dispatch recipients = Dispatch.call(mail, "Recipients").getDispatch(); 
            Dispatch.call(recipients, "Add" , receiver); 
            Dispatch.put(mail, "CC",cc);
            Dispatch.put(mail, "Subject", subject);
            Dispatch.put(mail, "Body", body);

            if(attachments.length>0)
            {
                Dispatch attachs = Dispatch.get(mail, "Attachments").toDispatch();

                for(Object attachment : attachments)
                {
                    Dispatch.call(attachs, "Add", attachment);
                }
            }

            Dispatch.call(mail, "Send");
        }
    }

Is there any possible way I can achieve that. I know a possible way to use a mailto: but got stuck on adding an attachment because basic requirement is to fill in the email address,subject,cc,message and attachment.

For sending emails I've used javax.mail library, its Java's library and its really simple to use. Don't see a need to go over JavaXComponent and to hook on Outlook library.

JavaMail (javax.mail) https://mvnrepository.com/artifact/javax.mail/mail/1.4.7

For simple mail sending and attachment sending, check out (as I mentioned before) javax.mail lib.

Import library, hook to You email, create simple message, add attachment(s), and Your good to go.

The Add method of the Attachments class accepts four parameter, one of them is required, others are optional.

  • Source - The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
  • Type - The type of the attachment. Can be one of the OlAttachmentType constants.
  • Position - This parameter applies only to e-mail messages using the Rich Text format: it is the position where the attachment should be placed within the body text of the message. A value of 1 for the Position parameter specifies that the attachment should be positioned at the beginning of the message body. A value 'n' greater than the number of characters in the body of the e-mail item specifies that the attachment should be placed at the end. A value of 0 makes the attachment hidden.
  • DisplayName - This parameter applies only if the mail item is in Rich Text format and Type is set to olByValue : the name is displayed in an Inspector object for the attachment or when viewing the properties of the attachment. If the mail item is in Plain Text or HTML format, then the attachment is displayed using the file name in the Source parameter.

For example, a sample VBA macro which illustrate a possible usage:

 Sub AddAttachment() 
   Dim myItem As Outlook.MailItem 
   Dim myAttachments As Outlook.Attachments 

   Set myItem = Application.CreateItem(olMailItem) 
   Set myAttachments = myItem.Attachments 
   myAttachments.Add "D:\Documents\Q496.xlsx", olByValue, 1, "4th Quarter 96 Results Chart" 
   myItem.Display 
 End Sub

So, basically you need to specify the full file path to the file you want to attach.

FYI Microsoft strongly recommends that developers find alternatives to Automation of Office if they need to develop server-side solutions. Because of the limitations to Office's design, changes to Office configuration are not enough to resolve all issues. Microsoft strongly recommends a number of alternatives that do not require Office to be installed server-side, and that can perform most common tasks more efficiently and more quickly than Automation. Before you involve Office as a server-side component in your project, consider alternatives. Read more about that in the Considerations for server-side Automation of Office article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM