《如何使用matlab发送html格式邮件》由会员分享,可在线阅读,更多相关《如何使用matlab发送html格式邮件(11页珍藏版)》请在金锄头文库上搜索。
1、Matlab发送html格式邮件Matlab是非常优秀的数据分析软件,具有很多优秀的功能,比如发送邮件。但是matlab自带的sendmail不支持html方式发送邮件正文,有时候非常不方便。查询可知matlab的sendmail和java的javax.mail package有密切关系,可以通过修改原函数的部分参数,扩展后就可以发送带html格式正文的邮件了。先截图看下效果:修改的部分为:将原函数的119-130行改为下图所示:修改前修改后此外将maxLineLength = 75;改为maxLineLength = inf;然后再按照原来函数的用法发送邮件就可以了。做法来源于:http:/
2、 sendmail(to,subject,theMessage,attachments)%SENDMAIL Send e-mail.% SENDMAIL(TO,SUBJECT,MESSAGE,ATTACHMENTS) sends an e-mail. TO is either a% character vector specifying a single address, or a cell array of character vector% addresses. SUBJECT is a character vector. MESSAGE is either a character vec
3、tor or a cell% array of character vectors. If it is a character vector, the text will% automatically wrap at 75 characters. If it is a cell array, it wont wrap, but% each cell starts a new line. In either case, use char(10) to explicitly specify% a new line. ATTACHMENTS is a character vector or a ce
4、ll array of character% vectors listing files to send along with the message. Only TO and SUBJECT are% required.% SENDMAIL relies on two preferences, Internet:SMTP_Server, your mail server, and% Internet:E_mail, your e-mail address. Use SETPREF to set these before using% SENDMAIL. The easiest ways to
5、 identify your outgoing mail server is to look at% the preferences of another e-mail application or consult your administrator. If% you cannot find out your servers name, setting it to just mail might work. If% you do not set these preferences SENDMAIL will try to determine them% automatically by re
6、ading environment variables and the Windows registry.% Example:% setpref(Internet,SMTP_Server,);% setpref(Internet,E_mail,);% sendmail(,Calculation complete.)% sendmail(,,Youre cool!, .% See the attached files for more info.,attach1.m,d:attach2.doc);% sendmail(,Adding additional breaks,one 10 two);%
7、 sendmail(,Specifying exact lines,one,two);% See also WEB, FTP.% Copyright 1984-2016 The MathWorks, Inc.% This function requires Java.if usejava(jvm) error(message(MATLAB:sendmail:NoJvm);endimport javax.mail.*import javax.mail.internet.*import javax.activation.*% Argument parsing.narginchk(2,4);if i
8、schar(to) to = to;endif (nargin 3) theMessage = ;endif (nargin 4) attachments = ;elseif ischar(attachments) attachments = attachments;end% Determine server and from.server,from = getServerAndFrom;if isempty(server) commandStr = setpref(Internet,SMTP_Server,); error(message(MATLAB:sendmail:SMTPServer
9、Indeterminate,commandStr);endif isempty(from) commandStr = setpref(Internet,E_mail,); error(message(MATLAB:sendmail:FromAddressIndeterminate,commandStr);end% Use the system properties, but clone them so we dont alter them.props = java.lang.System.getProperties.clone;props.put(mail.smtp.host,server);
10、% Create session.username = getpref(Internet,SMTP_Username,);password = getpref(Internet,SMTP_Password,);if isempty(username) pa = ;else pa = com.mathworks.util.PasswordAuthenticator(username,password);endsession = Session.getInstance(props,pa);% Create the theMessage.msg = MimeMessage(session);% Se
11、t sender.msg.setFrom(getInternetAddress(from);% Set recipients.for i = 1:numel(to) msg.addRecipient(getRecipientTypeTo(msg), . getInternetAddress(toi);end% Try to do the right thing on Japanese machines.isJapanese = ispc & strncmpi(matlab.internal.display.language,ja,2);% If charset is specified in
12、preferences, then use itcharset = ;if ispref(Internet, E_mail_Charset) charset = getpref(Internet, E_mail_Charset); elseif isJapanese charset = UTF-8;end% Set subject.if any(subject = char(10) | any(subject = char(13) error(message(MATLAB:sendmail:InvalidSubject);endif isempty(charset) msg.setSubjec
13、t(subject, charset)else msg.setSubject(subject)end% Set other headers.msg.setHeader(X-Mailer, MATLAB version)msg.setSentDate(java.util.Date);% Construct the body of the message and attachments.body = formatText(theMessage);isHtml = isempty(body) & body(1) = ; % msg starting with indicates HTMLif isHtml if isempty(charset) charset = text/html; charset=utf-8; else charset = text/html; charset= charset; endendif numel(attachments