Linux新浪发邮件加不了附件,Send mail with attachment from command line in unix/linux/Linux 下如何发送带附件的邮件...

Issue:

-----------------------------------------------------------------------------------------------------------------------

Send mail with attachment from command line in unix/linux

Linux 下如何发送带附件的邮件

Solution:

-----------------------------------------------------------------------------------------------------------------------

如果你可以使用 X window, 那么可以用 netscape 的

mail,也可以使用 kmail, 如果有的话。

如果这两个都不能用, 那么可以用 mail命令。

如果你能确认你的邮件服务器支持 8-bit 的字节,

可以直接用

cat ; | mail

;

不过很多邮件服务器不支持 8-bit 字节,

所以最好使用 uuencode 命令。

先写好邮件, 比如叫 mymail, 然后

uuencode ;

;

>;>; mymail

或者

cat ; | uuencode

;

>;>; mymail

然后编辑你的 mymail 文件, 在前面写上信的正文。

然后寄出。

收到信后, 把信中属于 mymail 的部分拷贝出来,

存为 themail.uue。如果对方是在 windows 下, 就可以用

winzip 7.0 解压, 即可得到附件。如果对方在 Linux

下, 可以用 uudecode 还原:

uudecode -o ;

themail.uue

cat ; | mail

;

如果有多个文件要处理,

那么就重复执行上述步骤,

不过这样收到邮件后对每个邮件需要分别处理。

当然压缩一下会得到更好的效果,

这样邮件长度会大大降低。时附件文件名就是压缩后的文件名。

方法1.uuencode file1 file1 | mail -s "title" mail@address

方法2.cat mailcontent.txt | mutt -s "title" -a attachfile1

mail@address

Discussion:

-----------------------------------------------------------------------------------------------------------------------

[root@linux ~]#[利用 uuencode 編碼 ] | [利用 mail

寄出去]

[root@linux ~]#uuencode [實際檔案] [信件中的檔名] | mail

-s '標題' email ###这里uuencode有两个参数,一般情况下容易犯的错误是只加一个参数(也就是文件的path),而不加”信件中的档案名“,所以命令就会hang在那里,按Ctrl+C后,你会收到邮件,但是没有附件。

範例一:將 ~/.bashrc 以附件夾帶的方式寄給

dmtsai

[root@linux ~]# uuencode ~/.bashrc bashrc | mail -s 'test encode'

dmtsai

http://www.cnblogs.com/lettoo/archive/2010/10/14/1850934.html

If you look at <

cookbook>> chapter 15.15

<

Script>>, you will find very detailed

instruction on this topic

-----------------------------------------------------------------------------

uuencode, uudecode—encodes a binary file into ASCII text

in order to send it through e-mail, or converts it back into its

original formuuencode[ source–file ] file–label

uudecode [ encoded–file ]

uuencode converts a binary file into an ASCII-encoded

representation that can be sent using mail. The label argument

specifies the output filename to use when decoding. If no file is

given, stdin is encoded. uudecode reads an encoded file, strips off

any leading and trailing lines added by mailer programs, and

re-creates the original binary data with the filename and the mode

and owner specified in the header. The encoded file is an ordinary

ASCII text file; it can be edited by any text editor. But it is

best only to change the mode or file label in the header to avoid

corrupting the decoded binary.

Example A.68.

1 uuencode mybinfile decodedname >

uumybinfile.tosend

2 uudecode uumybinfile.tosend

EXPLANATION

The first argument, mybinfile, is the existing file to be

encoded. The second argument is the name to be used for the

uudecoded file, after mailing the file, and uumybinfile.tosend is

the file that is sent through the mail.

This decodes the uuencoded file and creates a filename as given

as the second argument to uuencode.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于使用C语言的socket和SMTP协议发送附件邮件,我可以给你一些指导。 首先,SMTP协议是用于发送邮件的标准协议,它使用TCP连接进行通信。要发送附件邮件,你需要构建一个符合RFC 5322标准的邮件消息。这个消息包括邮件头和邮件体两部分,邮件头包含邮件的主题、发件人、收件人等信息,而邮件体则包含邮件的具体内容。 在C语言中,你可以使用socket库来建立TCP连接,并通过SMTP协议发送邮件。具体步骤如下: 1. 建立TCP连接到SMTP服务器的25端口。 2. 发送SMTP命令EHLO来与服务器建立联系。 3. 发送SMTP命令AUTH LOGIN来进行身份验证。 4. 发送发件人信息,例如:MAIL FROM:<sender@example.com> 5. 发送收件人信息,例如:RCPT TO:<recipient@example.com> 6. 发送DATA命令,表示邮件体的开始。 7. 发送邮件头和邮件体。 8. 发送附件,可以使用Base64编码将附件转换为文本格式并插入邮件体中。 9. 发送"."命令表示邮件发送结束。 10. 发送QUIT命令关闭连接。 这些步骤可以通过使用C语言的socket库和SMTP命令来实现。你可以使用类似以下代码的方式来发送邮件: ``` #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define MAXLINE 1024 int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; if (argc != 2) { printf("usage: %s <smtp server>\n", argv[0]); return 1; } sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(25); inet_pton(AF_INET, argv[1], &servaddr.sin_addr); connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); // 发送SMTP命令EHLO send(sockfd, "EHLO\r\n", 6, 0); recv(sockfd, recvline, MAXLINE, 0); // 发送SMTP命令AUTH LOGIN send(sockfd, "AUTH LOGIN\r\n", 12, 0); recv(sockfd, recvline, MAXLINE, 0); // 发送发件人信息 send(sockfd, "MAIL FROM:<sender@example.com>\r\n", 32, 0); recv(sockfd, recvline, MAXLINE, 0); // 发送收件人信息 send(sockfd, "RCPT TO:<recipient@example.com>\r\n", 34, 0); recv(sockfd, recvline, MAXLINE, 0); // 发送DATA命令 send(sockfd, "DATA\r\n", 6, 0); recv(sockfd, recvline, MAXLINE, 0); // 发送邮件头和邮件send(sockfd, "From: sender@example.com\r\n", 27, 0); send(sockfd, "To: recipient@example.com\r\n", 29, 0); send(sockfd, "Subject: Test email\r\n", 21, 0); send(sockfd, "Content-Type: text/plain; charset=UTF-8\r\n\r\n", 45, 0); send(sockfd, "This is a test email.\r\n", 23, 0); // 发送附件 send(sockfd, "Content-Type: application/octet-stream; name=test.txt\r\n", 57, 0); send(sockfd, "Content-Disposition: attachment; filename=test.txt\r\n", 56, 0); send(sockfd, "Content-Transfer-Encoding: base64\r\n\r\n", 38, 0); send(sockfd, "VGhpcyBpcyBhIHRlc3QgZW1haWwK", 31, 0); // 这里是附件内容的Base64编码 // 发送"."命令表示邮件发送结束 send(sockfd, "\r\n.\r\n", 5, 0); recv(sockfd, recvline, MAXLINE, 0); // 发送QUIT命令关闭连接 send(sockfd, "QUIT\r\n", 6, 0); close(sockfd); return 0; } ``` 这只是一个简单的示例,实际上你需要根据自己的需求来构建邮件消息和附件内容,并处理服务器返回的信息和错误情况。同时,由于邮件发送涉及到身份验证和敏感信息,你需要确保代码的安全性和正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值