锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 鍛冿紝濂藉惂錛屾垜鍙啓榪囦笁涓狫avaMail鐨勭▼搴忋?/p>
閮芥槸鎵歸噺閭歡瀹堟姢紼嬪簭銆傚喅瀹氭葷粨涓涓嬶紝甯屾湜闈炲父騫歌繍鎵懼埌榪欑瘒鏂囩珷鐨勪漢涓嶄細鍐嶅榪欎釜鍥版儜銆?/p>
Simple Mail Transfer Protocol (SMTP), documented in RFC
821, is Internet’s standard host-to-host mail transport protocol and
traditionally operates over TCP, port 25. In other words, a UNIX user
can type telnet hostname 25 and connect with an SMTP server, if one is
present. SMTP uses a style of asymmetric request-response protocol popular in
the early 1980s, and still seen occasionally, most often in mail
protocols. The protocol is designed to be equally useful to either a
computer or a human, though not too forgiving of the human. From the
server’s viewpoint, a clear set of commands is provided and
well-documented in the RFC. For the human, all the commands are clearly
terminated by newlines and a HELP command lists all of them. From the
sender’s viewpoint, the command replies always take the form of text
lines, each starting with a three-digit code identifying the result of
the operation, a continuation character to indicate another lines
following, and then arbitrary text information designed to be
informative to a human. 浜嬪疄涓婏紝浣犲彲浠ュ儚浣跨敤DOS鍛戒護涓鏍峰彂閫佺數(shù)瀛愰偖浠躲?a >http://bbs.stcore.com/archiver/tid-8024.htm 褰撶劧鍥犱負鍚勭鍘熷洜錛屼綘鐨勫皾璇曚笉鍙兘鎴愬姛銆備簨瀹炰笂SMTP宸ヤ綔鐨勬椂鍊欏氨鏄畝鍗曠殑鍙戦佸懡浠ゃ傚彇寰楄璇侊紝鍙戦佹暟鎹傚緱鍒板弽棣堛傜‘璁ら鍑鴻繖涔堢畝鍗曘?
鍦ㄨ繖縐嶇紪鐮佷腑錛屾垜浠皢瀛楃鎴栬呬簩榪涘埗緙栫爜浠?涓瘮鐗逛綅涓轟竴緇勶紝鏇挎崲鎴愮浉搴旂殑瀛楃褰㈠紡銆傛瘮濡?/p>
杞崲緇撴灉灝辨槸 浜庢槸錛屾垜浠氨鍙互浠ユ枃鏈殑鏂瑰紡緙栫爜浜岃繘鍒舵祦浠ュ強鎵╁睍ASCII瀛楃錛屾瘮濡備腑鏂囧瓧絎︺?/p>
鍩虹鐭ヨ瘑瀹屾瘯錛屼笅闈㈡槸FAQ mail.jar 閫氬父榪樹細闇瑕?activation.jar 涓嬭澆鍦板潃 鍏充簬錛?/p>
璇鋒煡鐪嬩互涓嬩唬鐮?/p>
榪欐槸涓婇潰鐢ㄦ埛楠岃瘉鐢ㄥ埌鐨勭被 What causes an
“javax.activation.UnsupportedDataTypeException: no object DCH for MIME
type xxx/xxxx javax.mail.MessagingException: IOException while sending
message;” to be sent and how do I fix this? [This happens for known
MIME types like text/htm 浜嬪疄涓婅繖涓槸閭歡鍙戦佹椂楠岃瘉緇勪歡璁劇疆涓嶅綋寮曡搗鐨勶紝榪欎釜緇勪歡閰嶇疆鏂規(guī)硶濡備笅 (http://java.sun.com/j2ee/1.4/docs/api/javax/activation/MailcapCommandMap.html) The MailcapCommandMap looks in various places in the user’s system
for mailcap file entries. When requests are made to search for commands
in the MailcapCommandMap, it searches mailcap files in the following
order: 1) Programatically added entries to the MailcapCommandMap instance. 鎴戦夌敤浜嗙鍥涚鏂規(guī)硶錛屽湪鐢熸垚鐨凧ar鏂囦歡涓姞鍏ヤ簡 META-INF/mailcap.
9&&7 will not compile. Understand the difference between short-circuit logical operators and bitwise
operators.
expression of a || operator is true, the second expression is not evaluated.Look Out
you can ignore the sign of the second operand.Memorize
]]>
Look Out
Memorize
]]>蹇呴』鏄庣櫧鐨勫熀紜鐭ヨ瘑
鍗忚鐨勬爣鍑嗗湪榪欓噷 http://www.ietf.org/rfc/rfc2821.txt?number=2821
涓嬮潰鏄壖瑕佽鏄庯紙http://www.freesoft.org/CIE/Topics/94.htm 錛夛細
SMTP涓彂閫佺殑鏁版嵁錛岄伒浠?font size="-1">Multipurpose Internet Mail Extensions (MIME)鏍囧噯錛屽憙錛屾垜涓嶅緱涓嶈錛岃繖鏄繖涓槦鐞冧笂鏈閲嶈鐨勬爣鍑嗕箣涓銆傛墍鏈夌殑浜掕仈緗戦氫俊鍩烘湰閮芥槸鍩轟簬榪欎釜鏍囧噯鐨勬紨鍖栥傞櫎浜嗙數(shù)瀛愰偖浠訛紝甯歌鐨勫簲鐢ㄨ繕鍖呮嫭HTTP鎶ユ枃絳夛紙涔熷氨鏄墍鏈夌綉欏典簡錛夛紝鍙﹀鍗充嬌鍦?0騫村悗鍙戝睍鐨刋ML錛屽叾2榪涘埗鏁版嵁鍙戦佷粛鐒跺疄鐢ㄧ殑MIME涓殑緙栫爜鏂瑰紡銆?br>
鎭╋紝榪欓噷灝辨秹鍙婂埌閭歡闄勪歡濡備綍澶勭悊鐨勯棶棰樸傛仼錛岀畝鍗曞湴璇村氨鏄疊ASE64緙栫爜
Table 1: The Base64 Alphabet
Value Encoding Value Encoding Value Encoding Value Encoding0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y100110111010001011101001
100110 -> 38
111010 -> 58
001011 -> 11
101001 -> 4138 -> m
58 -> 6
11 -> L
41 -> pm6Lp
Java鍙戦佺數(shù)瀛愰偖浠墮渶瑕佸摢浜涜蔣浠跺寘
http://java.sun.com/products/javabeans/jaf/downloads/index.html
https://maven-repository.dev.java.net/nonav/repository/javax.mail/濡備綍鍙戦侀偖浠?/h3>
public static synchronized void sendMail(Properties settings)
throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", settings.get(StartCore.MAIL_SERVER));
props.put("mail.smtp.user", settings.get(StartCore.USER_NAME));
props.put("mail.smtp.auth", "true");
//SMTP鏈嶅姟鍣ㄧ敤鎴烽獙璇?/span>
Authenticator auth = new SMTPAuthenticator((String) settings
.get(StartCore.USER_NAME), (String) settings
.get(StartCore.PASSWORD));
Session session = Session.getDefaultInstance(props, auth);
if ("true".compareToIgnoreCase((String) settings.get("DEBUG")) == 0) {
session.setDebug(true);
}
//鍒涘緩娑堟伅浣?/span>
MimeMessage msg = new MimeMessage(session);
//璁劇疆鍙戦佷漢閭歡
msg.setFrom(new InternetAddress((String) settings
.get(StartCore.USER_MAIL)));
//璁劇疆鎺ユ敹浜洪偖浠?/span>
address = new InternetAddress[] { new InternetAddress(rs
.getString("GRE_mail")) };
msg.setRecipients(Message.RecipientType.TO, address);
//璁劇疆涓婚錛屼腑鏂囩紪鐮?/span>
msg.setSubject(subject, "gbk");
msg.setSentDate(new Date());
String content = "閭歡姝f枃";
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(content, "gbk");
//閭歡闄勪歡
MimeBodyPart attachFilePart = new MimeBodyPart();
File file = new File("涓枃闄勪歡.txt");
FileDataSource fds = new FileDataSource(file.getName());
attachFilePart.setDataHandler(new DataHandler(fds));
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
//瑙e喅涓枃闄勪歡鍚嶇О
attachFilePart.setFileName("=?gbk?B?"
+ enc.encode(file.getName().getBytes("gbk")) + "?=");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(attachFilePart);
msg.setContent(mp);
// send the message
msg.saveChanges();
Transport.send(msg);
}class SMTPAuthenticator extends javax.mail.Authenticator {
private String username;
private String password;
/**
* @param username
* @param password
*/
public SMTPAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}閭歡鍙戦佸嚭閿?/h3>
2) The file .mailcap in the user’s home directory.
3) The file /lib/mailcap.
4) The file or resources named META-INF/mailcap.
5) The file or resource named META-INF/mailcap.default (usually found only in the activation.jar file).#
# This is a very simple 'mailcap' file
#
image/gif;; x-java-view=com.sun.activation.viewers.ImageViewer
image/jpeg;; x-java-view=com.sun.activation.viewers.ImageViewer
text/*;; x-java-view=com.sun.activation.viewers.TextViewer
text/*;; x-java-edit=com.sun.activation.viewers.TextEditor
text/html;; x-java-content-handler=com.sun.mail.handlers.text_html
text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml
text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain
multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed
message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822
]]>
For instance, according to the Java class library documentation, the read( ) method of java.io.InputStream returns "the next byte of data, or -1 if the end of the stream is reached." Upon reflection, this sounds suspicious. How is a -1 that appears as part of the stream data to be distinguished from a -1 indicating end of stream? In point of fact, the read( ) method does not return a byte; its signature shows that it returns an int:
This int is not a Java byte with a value between -128 and 127 but a more general unsigned byte with a value between 0 and 255. Hence, -1 can easily be distinguished from valid data values read from the stream.
JVM and byte, byte array
In fact, a single byte still takes up four bytes of space inside the Java virtual machine, but a byte array occupies only the amount of space it actually needs. The virtual machine includes special instructions for operating on byte arrays but does not include any instructions for operating on single bytes. They're just promoted to ints.
Input, Output, Reader and Writer
For the most part, these classes have methods that are extremely similar to the equivalent stream classes. Often the only difference is that a byte in the signature of a stream method becomes a char in the signature of the matching reader or writer method. For example, the java.io.OutputStream class declares these three write( ) methods:
The java.io.Writer class, therefore, declares these three write( ) methods:
As you can see, the signatures match except that in the latter two methods the byte array data has changed to a char array. There's also a less obvious difference not reflected in the signature. While the int passed to the OutputStream write( ) method is reduced modulo 256 before being output, the int passed to the Writer write( ) method is reduced modulo 65,536. This reflects the different ranges of chars and bytes.
java.io.Writer also has two more write( ) methods that take their data from a string:
public void write(String s)
throws IOException
public void write(String s, int offset, int length) throws IOException
Because streams don't know how to deal with character-based data, there are no corresponding methods in the java.io.OutputStream class.
Hello World!
聽
Powered by Zoundry