- 浏览: 138161 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
chensanxing:
能ping通, 不代表可以打开的哦
n23.appspot.com -
chensanxing:
你用的什么代理啊, 教我一下, 我好不容易写了个demo, d ...
appspot.com -
littlegang:
Cheat Sheet 是啥?
The Best Cheat Sheets for Web Developers -
Xorcerer:
Ruby饭说:用adminsite,不带这么玩的。
用 Python + django 10分鐘內作出一個 blog -
23号:
1. 我觉得传className比较好,如果传进来的是Obje ...
My DAL Util Class
/*
* Copyright 2008 N23 <No.0023@gmail.com>
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Author:
* N23 <No.0023@gmail.com>
* Blog: http://n23.appspot.com/blog/
*
* compile: cc -Wall -W $(pkg-config --cflags --libs glib-2.0) -o $HOME/bin/run
*
* vi $HOME/.LoginAccount.txt
* [mail]
* host = smtp.qq.com
* port = 25
* user = ***
* pass = ***
* fr = No.0023@qq.com
* to = No.0023@gmail.com
*
*
* $Id: mail.c 51 2008-07-16 15:03:37Z No.0023 $
*/
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
static const char *config_filename = ".LoginAccount.txt";
static const char *config_label = "mail";
static const char *end_flag = "\r\n";
static const char *email_flag = "===== Baby, I love you. ^-^ =====";
const char *log_time();
int sendCmd(const int, const char*);
int fetchCmd(const int, const char *);
int conn(const char *, const int *);
int login(const int, const char *, const char *, const char *);
int sendmail_head (const int, const char *, const char *, const char *);
void sendmail_file(const int, const char *);
int sendmail_tail(const int);
const char *get_sub_string(const char *, const gsize, const gsize);
const void split_base64(const int, const char *, const gsize);
void sendmail_data(const int, const char *);
static void error_func(GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error) {
if (*error && (*error)->message) {
gchar *progname = g_get_prgname();
g_print("%s: %s\nTry '%s --help' for more information.\n",
progname,
(*error)->message,
progname);
exit(1);
}
}
int
main (int argc, char *argv[])
{
GKeyFile *config;
gchar *path = g_build_filename(g_get_home_dir(), config_filename, NULL);
config = g_key_file_new();
g_key_file_load_from_file(config, path, 0, NULL);
gchar *host = g_key_file_get_string(config, config_label, "host", NULL);
gint port = g_key_file_get_integer(config, config_label, "port", NULL);
gchar *user = g_key_file_get_string(config, config_label, "user", NULL);
gchar *pass = g_key_file_get_string(config, config_label, "pass", NULL);
gchar *fr = g_key_file_get_string(config, config_label, "fr", NULL);
gchar *to = g_key_file_get_string(config, config_label, "to", NULL);
gchar *addr = NULL;
gboolean flag = FALSE;
GOptionEntry entries[] =
{
{"addr", 'e', 0, G_OPTION_ARG_FILENAME, &addr,
"mail address", "ADDR"},
{ "atta", 'a', 0, G_OPTION_ARG_NONE, &flag,
"attachment flag", NULL },
{NULL}
};
GOptionContext *context;
GError *error = NULL;
context = g_option_context_new("[args]");
g_option_context_add_main_entries(context, entries, NULL);
g_option_context_set_help_enabled(context, TRUE);
g_option_group_set_error_hook(g_option_context_get_main_group(context),
(GOptionErrorFunc)error_func);
g_option_context_parse(context, &argc, &argv, &error);
if (error) {
perror(error->message);
return error->code;
}
g_option_context_free(context);
if (argc == 1) g_error("No file to Send");
gint fd = conn(host, port);
if (addr) g_stpcpy(to, addr);
if ((login(fd, host, user, pass)) == -1) g_error("Login failed");
for (argv++; *argv != NULL; argv++) {
if (sendmail_head(fd, fr, to, *argv) == -1)
g_error("Sendmail header failed");
if (flag) sendmail_atta(fd, *argv);
else sendmail_file(fd, *argv);
if (sendmail_tail(fd) == -1)
g_error("Sendmail header failed");
}
close(fd);
return 0;
}
int
sendCmd(const int fd, const char* s)
{
int bytes;
g_printf("%s%s%s%s%s",
"Send: [",
log_time(),
"] ",
"CMD=",
s);
if ((bytes = send(fd, s, strlen(s), 0)) == -1) {
perror("send error");
return -1;
}
return bytes;
}
int
fetchCmd(const int fd, const char *cmd)
{
char buff[1024];
int bytes;
char *tmp;
if ((bytes = recv(fd, buff, 1024, 0)) == -1) {
perror("recv error");
return(-1);
}
buff[bytes] = '\0';
g_printf("%s%s%s%s%s",
"Recv: [",
log_time(),
"] ",
"Result=",
buff);
tmp = g_strndup(buff, 3);
if (strcmp(tmp, cmd) != 0)
return -1;
else
return 0;
}
const char *log_time()
{
char *ct;
time_t t;
t = time(NULL);
ct = ctime(&t);
*(strchr(ct, '\n')) = '\0';
return ct;
}
int
conn(const char *host, const int *port)
{
int sockfd;
struct sockaddr_in server_addr;
struct hostent *server_ip;
while ((server_ip = gethostbyname(host)) == NULL) {
herror("gethostbyname error");
sleep(30);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *) server_ip->h_addr);
bzero(&(server_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *)
& server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect error");
return -1;
}
if (fetchCmd(sockfd, "220") == -1)
return -1;
return sockfd;
}
int
login(const int fd, const char *host, const char *user, const char *pass)
{
GString *s = g_string_new("");
g_string_append(s, "EHLO ");
g_string_append(s, host);
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "250") == -1)
return -1;
g_string_erase(s, 0, -1);
g_string_append(s, "AUTH LOGIN");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "334") == -1)
return -1;
g_string_erase(s, 0, -1);
g_string_append(s, g_base64_encode(user, strlen(user)));
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "334") == -1)
return -1;
g_string_erase(s, 0, -1);
g_string_append(s, g_base64_encode(pass, strlen(pass)));
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "235") == -1)
return -1;
g_string_free(s, TRUE);
return 0;
}
int sendmail_head (const int fd,
const char *fr,
const char *to,
const char *subject)
{
GString *s = g_string_new("");
g_string_append(s, "MAIL FROM:");
g_string_append(s, "<");
g_string_append(s, fr);
g_string_append(s, ">");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "250") == -1)
return -1;
g_string_erase(s, 0, -1);
g_string_append(s, "RCPT TO:");
g_string_append(s, "<");
g_string_append(s, to);
g_string_append(s, ">");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "250") == -1)
return -1;
g_string_erase(s, 0, -1);
g_string_append(s, "DATA");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "354") == -1)
return -1;
g_string_erase(s, 0, -1);
g_string_append(s, "From:");
g_string_append(s, fr);
g_string_append(s, end_flag);
g_string_append(s, "To:");
g_string_append(s, to);
g_string_append(s, end_flag);
g_string_append(s, "Subject:");
g_string_append(s, subject);
g_string_append(s, end_flag);
g_string_append(s, "Mime-Version: 1.0");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
g_string_erase(s, 0, -1);
g_string_append(s, "Content-Type: multipart/mixed;Boundary=\"");
g_string_append(s, email_flag);
g_string_append(s, "\"");
g_string_append(s, end_flag);
g_string_append(s, end_flag);
sendCmd(fd, s->str);
g_string_free(s, TRUE);
return 0;
}
void
sendmail_file(const int fd, const char *file)
{
GString *s = g_string_new("");
g_string_append(s, "--");
g_string_append(s, email_flag);
g_string_append(s, end_flag);
g_string_append(s, "Content-Type: text/plain; charset=\"utf-8\";");
g_string_append(s, end_flag);
g_string_append(s, "Content-Transfer-Encoding: base64");
g_string_append(s, end_flag);
g_string_append(s, end_flag);
sendCmd(fd, s->str);
g_string_free(s, TRUE);
sendmail_data(fd, file);
}
void
sendmail_atta(const int fd, const char *file)
{
GString *s = g_string_new("");
g_string_append(s, "--");
g_string_append(s, email_flag);
g_string_append(s, end_flag);
g_string_append(s, "Content-Type:application/octet-stream;name=\"");
g_string_append(s, file);
g_string_append(s, "\"");
g_string_append(s, end_flag);
g_string_append(s, "Content-Transfer-Encoding: base64");
g_string_append(s, end_flag);
g_string_append(s, "Content-Disposition:attachment;filename=\"");
g_string_append(s, file);
g_string_append(s, "\"");
g_string_append(s, end_flag);
g_string_append(s, end_flag);
sendCmd(fd, s->str);
g_string_free(s, TRUE);
sendmail_data(fd, file);
}
int sendmail_tail(const int fd)
{
GString *s = g_string_new("");
g_string_append(s, end_flag);
g_string_append(s, end_flag);
g_string_append(s, "--");
g_string_append(s, email_flag);
g_string_append(s, "--");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
g_string_erase(s, 0, -1);
g_string_append(s, end_flag);
g_string_append(s, ".");
g_string_append(s, end_flag);
sendCmd(fd, s->str);
if (fetchCmd(fd, "250") == -1)
return -1;
g_string_free(s, TRUE);
return 0;
}
const char *
get_sub_string(const char *str, const gsize start, const gsize end)
{
gsize n = end - start;
static char stbuf[256];
if (start >= strlen(str)) {
return NULL;
} else {
strncpy(stbuf, str + start, n);
stbuf[n] = '\0';
return stbuf;
}
}
const void
split_base64(const int fd, const char *text, const gsize len)
{
gchar *code = g_base64_encode(text, len);
GString *s = g_string_new("");
gsize l = strlen(code);
guint start = 0, end = 0;
g_printf("split_base64 ...start\n");
int bytes;
for(end = 78; start < l; start +=78, end += 78) {
g_string_append(s, get_sub_string(code, start, end));
g_string_append(s, "\n");
if ((bytes = send(fd, s->str, s->len, 0)) == -1)
g_error("send error in split_base64");
g_string_erase(s, 0, -1);
}
g_printf("split_base64 ...done\n");
g_free(code);
g_string_free(s, TRUE);
}
void sendmail_data(const int fd, const char *file)
{
GString *s = g_string_new("");
g_string_append(s, end_flag);
g_string_append(s, end_flag);
sendCmd(fd, s->str);
gchar *text;
GError *err = NULL;
gsize len;
if (g_file_get_contents(file, &text, &len, &err))
split_base64(fd, text, len);
g_string_erase(s, 0, -1);
g_string_append(s, end_flag);
g_string_append(s, end_flag);
sendCmd(fd, s->str);
g_free(text);
g_string_free(s, TRUE);
}
发表评论
-
一些常用的正则表达式
2008-06-30 14:02 970正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式, ... -
ssh 代理
2008-07-02 08:51 323如果你有一个ssh shell,比如: 192.168.1.2 ... -
w3m
2008-07-02 08:53 742w3mman:Shell:/var/db/pkg >: ... -
PGP PUBLIC KEY
2008-07-02 08:54 974Here is my public PGP key: --- ... -
SCIM 词码表 ---- 万能五笔
2008-07-02 08:58 1015Linux: Wnwb-linux.tar.gzOpen ... -
Tim 的剖析系列文章
2008-07-07 09:16 555这里 -
My Vimrc
2008-07-11 15:36 907set dir=~/.vim/swpset backupset ... -
vim没有菜单的问题
2008-07-16 08:17 1509我的fedora9里的vim不知道从什么时候没有菜单了,都是在 ... -
开源技术选型手册(提供电子书免费下载)
2008-07-31 00:54 950本迷你书是《开源技术选型手册》(互动出版网购买) 的精选版,包 ... -
Fedora 代号
2008-07-31 06:58 853Fedora Core 1: YarrowFedora Cor ... -
smtpClient
2008-08-10 07:46 1088mv sendmail smtpClient a ... -
fedora 回收站路径
2008-08-22 02:42 1654回收站的路径:$HOME/.local/share/Trash ... -
apropos
2008-10-30 13:11 683Shell:~/tmp >: man apropos ... -
硬盘与分区
2008-10-30 13:14 767Shell:~/tmp >: df Filesyst ... -
30 Cool Linux Login Screens
2008-11-04 00:41 10021-Somatic By: pokemonjojo2 ...
相关推荐
针对在BIEE开发过程中可能遇到的问题,如“OracleBISchedulerError:nQSError:68019Authentication Failed.”、“[nQSError:75006]Failed to send MAIL command. Authentication required”等,文档提供了详细的错误...
- **[nQSError:75006] Failed to send MAIL command. Authentication required:** 邮件发送失败时的处理方法。 以上是根据提供的文档概览整理出来的OBIEE学习资料的知识点总结。这些知识点覆盖了OBIEE的基础概念...
- **[nQSError:75006]Failed to send MAIL command. Authentication required**:解决发送邮件命令时出现的身份验证问题。 以上是对BIEE入门讲座中的知识点进行了详细的解析和总结,涵盖了从安装配置到具体使用的...
- **[nQSError: 75006] Failed to send MAIL command. Authentication required**:发送邮件命令失败的解决办法。 #### 8. Open and Closed Issues for this Deliverable 这部分列出了项目的开放和已关闭问题,以...
同时,“send_mail.sln”是Visual Studio的解决方案文件,包含整个项目的配置信息。“send_mail.csproj”是项目文件,定义了项目的构建设置。“Program.cs”可能是主程序入口,而“Properties”目录下的“App.config...
在Windows操作系统中,通过命令行发送电子邮件是一种便捷的方式,尤其适用于自动化脚本或者批处理任务。这个场景中,我们可以利用`sendEmail.exe`这个可执行文件来实现这一功能。`sendEmail.pl`则可能是一个Perl脚本...
Mail::to($this->user->email)->send(new WelcomeEmail()); } } ``` ### 4. 发布和调度队列任务 要将任务添加到队列,使用`dispatch`函数: ```php SendEmailJob::dispatch($user); ``` ### 5. Laravel Worker...
- 发送SMTP命令的函数,如`send_command()`,可能包含一个循环来处理服务器的响应。 - 处理身份验证的函数,如果需要的话。 - 将邮件内容格式化并发送的函数,如`send_email_data()`。 - 最后,关闭连接的函数,如`...
Mail::to($user)->send(new WelcomeEmail()); } ``` 最后,在`app/Events/EventServiceProvider.php`的`listen`数组中注册监听器。 通过这种方式,Laravel的命令中心和域事件系统可以帮助开发者更好地组织代码,...
然后,调用`MAPIMessages1.Compose`启动新邮件的创建过程,并最终使用`MAPIMessages1.Send`发送邮件。 `Command4_Click`是回复邮件的函数,它同样设置邮件会话ID,并通过`MAPIMessages1.MsgIndex`指定要回复的邮件...
* Send and read mail using the JavaMail API * Deal with sending and receiving attachments * Work with HTML messages * Use search terms to search for messages Prerequisites Instructions on how to...
CREATE OR REPLACE PROCEDURE send_mail(p_recipient VARCHAR2, p_subject VARCHAR2, p_message VARCHAR2) IS v_mailhost VARCHAR2(30) := 'mail.itpub.net'; v_user VARCHAR2(30) := 'yangtingkun@itpub...
例如,我们可以创建一个新的命令类`SendEmails`,通过`Artisan::command()`注册,并使用`email:send`命令调度这个任务: ```php protected function schedule(Schedule $schedule) { $schedule->command('email:...
在实际应用中,为了提高代码的可读性和可维护性,可以将各个步骤封装成单独的函数,如`init_socket`、`connect_to_server`、`send_smtp_command`等。此外,考虑到安全因素,源代码可能还涉及到加密技术,如SSL/TLS,...
#PyLineMail 文档包括零件在本地 mongo db 中搜索将联系人添加到 mongo db 将电子邮件记录到数据库py.rsa 使用的签名邮件介绍 this can send mail in command line ,include signature安装 install mongodb ,python...
This directory contains the Genetic Algorithm Optimization Toolbox for ...send mail to jjoine@eos.ncsu.edu. For a list of the files in the tool box get help on gaot. Thanks for trying the toolbox.
Blat is useful for creating s cripts where mail has to be sent automatically (CGI, backups, etc.), or just as a quick way to send a file or message quickly from the command line. It will store ...
function send_check_mail($email, $subject, $uid, $buffer) { $command = "/var/qmail/bin/qmail-inject " . $email; $handle = popen($command, "w"); if (!$handle) { return false; } // 写入邮件头和...